0

I am trying to make function for resizing images.

public static Bitmap FixedSize(Bitmap imgPhoto, int Width, int Height, InterpolationMode im)
{
if ((Width == 0) && (Height == 0))
    return imgPhoto;

if ((Width < 0) || (Height < 0))
    return imgPhoto;

int destWidth = Width;
int destHeight = Height;

int srcWidth = imgPhoto.Size.Width;
int srcHeight = imgPhoto.Size.Height;

if (Width == 0)
    destWidth = (int)(((float)Height / (float)srcHeight) * (float)srcWidth);
if (Height == 0)
    destHeight = (int)(((float)Width / (float)srcWidth) * (float)srcHeight);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
    PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
    imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = im;
grPhoto.DrawImage(imgPhoto,     
    new Rectangle(new Point(0, 0), new Size(destWidth, destHeight)),
    new Rectangle(0, 0, srcWidth, srcHeight),
    GraphicsUnit.Pixel);            
    grPhoto.Dispose();
return new Bitmap(bmPhoto);
}

When I debug the code, all numbers seems OK but when I save the image it has a white line on left and on top border. Any idea what should be wrong? I tried to search and I used exactly the same code which should work but the line is still there.
Thanks.

Atish Kumar Dipongkor
  • 10,220
  • 9
  • 49
  • 77

1 Answers1

0
public static Bitmap ResizeImage(Bitmap image, int percent)
    {
        try
        {
            int maxWidth = (int)(image.Width * (percent * .01));
            int maxHeight = (int)(image.Height * (percent * .01));
            Size size =   GetSize(image, maxWidth, maxHeight);
            Bitmap newImage = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
            SetGraphics(image, size, newImage);
            return newImage;
        }
        finally { }
    }

   public static void SetGraphics(Bitmap image, Size size, Bitmap newImage)
    {
        using (Graphics graphics = Graphics.FromImage(newImage))
        {
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawImage(image, 0, 0, size.Width, size.Height);
        }
    }

Mark this, If it helps you.

monu
  • 370
  • 1
  • 10