Grey borders in DrawImage (.NET system.drawing.drawing2D)
Asked Answered
C

1

4

I'm trying to create an image on a bitmap using C#. Somehow it always adds grey border on all sides of the image. Many questions have been posted with the solution and I have tried almost all of them but none of them worked.

Code that I'm using:

Bitmap appearance = new Bitmap(490, 196, PixelFormat.Format32bppArgb);

using (Graphics graphics = Graphics.FromImage(appearance))
{
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.PixelOffsetMode = PixelOffsetMode.Half;
    graphics.CompositingMode = CompositingMode.SourceCopy;

    using (ImageAttributes wrapMode = new ImageAttributes())
    {
        wrapMode.SetWrapMode(WrapMode.TileFlipXY);
        Rectangle destination = new Rectangle(5, 99, 240, 94);
        graphics.DrawImage(img, destination, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, wrapMode);
    }
}
   
appearance.Save("F:\\SignatureAppearance.jpeg");

Can anyone help me? The grey border does not show when zoomed out but as one starts zooming in, borders start appearing.

enter image description here

Any help will be appreciated

i have tried to create an image with white background even that has right and bottom borders .

Bitmap appearance = new Bitmap(490, 196, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(appearance)) { 
// graphics.Clear(Color.White); 
   Rectangle ImageSize = new Rectangle(0, 0, 490, 196); 
   graphics.FillRectangle(Brushes.White, ImageSize); } 
    appearance.Save("F:\\Signature_Appearance.png", ImageFormat.Png);

}

enter image description here

Cohl answered 25/7, 2020 at 10:31 Comment(3)
Can this be done with any other library rather than using system.drawing as i have to create new image on server side with above details etc.Cohl
This question is incomplete. There is no mention of what img is. The border may very simply be inside that image. As for alternative, you should look into the System.Windows.Media.Imaging namespace.Balliett
Here is an in-depth article on DrawImage and what side effects it can cause: photosauce.net/blog/post/…Balliett
A
1

The problem appears to be that you haven't specified a save format, and the default save format for an image with PixelFormat.Format32bppArgb is PNG. The file extension on your path is ignored.

You would need to change your last line to

appearance.Save(@"F:\SignatureAppearance.jpeg", ImageFormat.Jpeg);

to actually get a JPEG output.

Additionally, it appears you may not have filled the entire Bitmap you created, unless that code is omitted from your example. To ensure a white background, you would need to fill any undefined pixels with white, because transparent pixels are mapped to black in the JPEG encoder. Your code snippet above only shows that the destination sub-area of the image is defined. You can fill the missing area by inserting

graphics.Clear(Color.White);

before you draw into the sub-rectangle.

Acrogen answered 26/7, 2020 at 20:22 Comment(2)
i have tried to create a simple image with white backgroud even that brought borders :( Bitmap appearance = new Bitmap(490, 196, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(appearance)) { Rectangle ImageSize = new Rectangle(0, 0, 490, 196); graphics.FillRectangle(Brushes.White, ImageSize); } appearance.Save("F:\\Signature_Appearance.png", ImageFormat.Png );Cohl
graphics.Clear(Color.White); works for me...thanksBabushka

© 2022 - 2024 — McMap. All rights reserved.