Grey borders in DrawImage (.NET system.drawing.drawing2D)

浪尽此生 提交于 2020-07-30 08:12:59

问题


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.

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);

}


回答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.



来源:https://stackoverflow.com/questions/63087257/grey-borders-in-drawimage-net-system-drawing-drawing2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!