3

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

Salman
  • 1,266
  • 5
  • 21
  • 41
  • 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. – Salman Jul 25 '20 at 11:17
  • 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. – Nyerguds Jul 27 '20 at 01:57
  • Here is an in-depth article on `DrawImage` and what side effects it can cause: https://photosauce.net/blog/post/image-scaling-with-gdi-part-3-drawimage-and-the-settings-that-affect-it – Nyerguds Jul 29 '20 at 11:16

1 Answers1

0

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.

saucecontrol
  • 1,446
  • 15
  • 17
  • 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 ); – Salman Jul 27 '20 at 07:35