Hello,
I have got a problem when trying to scale an image. The problem presents itself when the image i am trying to scale (original) is smaller than the size i am trying to scale up to.
For example, an image with the width of 850px and the height of 700px trying to be scaled up to 950px width and height. The image seems to be scaled properly but is beeing drawn wrong on my bitmap. The code to scale the image will follow. The width beeing sent into ScaleToFitInside is the width and height trying to scale to, 950px both in my example.
public static Image ScaleToFitInside(Image image, int width, int height) {
Image reszied = ScaleToFit(image, width, height);
Bitmap bitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bitmap)) {
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect = new Rectangle(0, 0, width, height);
g.FillRectangle(Brushes.White, rect);
int x = (int)(((float)width - (float)reszied.Width) / 2);
int y = (int)(((float)height - (float)reszied.Height) / 2);
Point p = new Point(x, y);
g.DrawImageUnscaled(reszied, p);
foreach (PropertyItem item in image.PropertyItems) {
bitmap.SetPropertyItem(item);
}
}
return bitmap;
}
public static Image ScaleToFit(Image image, int maxWidth, int maxHeight) {
int width = image.Width;
int height = image.Height;
float scale = Math.Min(
((float)maxWidth / (float)width),
((float)maxHeight / (float)height));
return (scale < 1) ? Resize(image, (int)(width * scale), (int)(height * scale)) : image;
}
public static Image Resize(Image image, int width, int height) {
Bitmap bitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bitmap)) {
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect = new Rectangle(0, 0, width, height);
g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
foreach (PropertyItem item in image.PropertyItems) {
bitmap.SetPropertyItem(item);
}
}
return bitmap;
}
So the picture gets scaled, but beeing drawn wrong on my bitmap causing the image to "fall" outside of my bitmap and not presenting its whole self.
Example: When sending in the values 2000x2000 (trying to upscale). The following happens.
Since the original picture is smaller than the upsizing values i do not want to "sacle it up", but to remain the same size. The effect i want is do draw a rectangle with the size of 2000x2000 and draw the image in the center of it.
My example picture produces these values:
width = 2000; height = 2000; resized.Width will be 1000 and resized.Height will be 737 (original pictures size).
x = (2000 - 1000) / 2 = 500; y = (2000 - 737) / 2 = 631.
When drawing these values onto a paper and matching it to the rectangle it seems to be the right values, but the image is still drawn on th wrong spot, not in the middle at all.
Thanks in advance.