Is there any convenient way of resizing image with C#?
+2
A:
Hi
The following link may help you out :)
snippets.dzone.com/posts/show/4336
Update: The code in the link above physically resizes and saves the image if your intention is to resize the image file in order to save space, otherwise as mentioned you could just manually set the height/width on the image control.
Dal
2009-10-01 13:59:53
A:
If you load up an image then you can just set the Width/Height properties.
e.g.
Bitmap bmp = new Bitmap(@"c:\myfile.bmp");
bmp.Width = (int)(bmp.Width / 2);
bmp.Height = (int)(bmp.Height / 2);
Ian
2009-10-01 14:00:06
A:
From my personal code gallery. Little bit old anyway.
protected Image FixedSize(Image imgPhoto, int Width, int Height)
{
int width = imgPhoto.Width;
int height = imgPhoto.Height;
int x = 0;
int y = 0;
int num5 = 0;
int num6 = 0;
float num7 = 0f;
float num8 = 0f;
float num9 = 0f;
num8 = ((float) Width) / ((float) width);
num9 = ((float) Height) / ((float) height);
if (num9 < num8)
{
num7 = num9;
num5 = Convert.ToInt16((float) ((Width - (width * num7)) / 2f));
}
else
{
num7 = num8;
num6 = Convert.ToInt16((float) ((Height - (height * num7)) / 2f));
}
int num10 = (int) (width * num7);
int num11 = (int) (height * num7);
Bitmap image = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
image.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics graphics = Graphics.FromImage(image);
graphics.Clear(ColorTranslator.FromHtml("#ffffff"));
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(imgPhoto, new Rectangle(num5, num6, num10, num11), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
graphics.Dispose();
return image;
}
NinethSense
2009-10-01 14:12:58
-1: num1, num2, num3, ... num11. No offense, but the choice of variable names reads like the code is purposefully obfuscated.
Juliet
2009-10-01 14:19:59
This is some serious WTF code: variable names, Convert.ToInt16, ColorTranslators.FromHtml. I'll refrain from down-voting because at least you got the InterpolationMode right.
MusiGenesis
2009-10-01 14:44:27
Kidooosss... this code is from .NET Reflector. I have the idea of copy-paste. Intention is to show only logic. rating... your choice. :-)
NinethSense
2009-10-05 10:18:47
+1
A:
In additiona to what others have said, for images smaller than 300x300, Image.GetThumbnailImage is very convenient:
static void Main(string[] args)
{
Bitmap b = new Bitmap(@"C:\Documents and Settings\juliet\My Documents\My Pictures\julietawesome.bmp");
Image image = b.GetThumbnailImage(100, 100, null, IntPtr.Zero);
image.Save(@"C:\Documents and Settings\juliet\My Documents\My Pictures\thumbnail.bmp", ImageFormat.Bmp);
}
Juliet
2009-10-01 14:14:00
+1
A:
Public Function ScaleByPercent(ByVal imgPhoto As Image, ByVal Percent As Integer) As Image
Dim nPercent As Single = (CType(Percent, Single) / 100)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer)
Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer)
Dim bmPhoto As New Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)
grPhoto.Dispose()
Return bmPhoto
End Function
quimbo
2009-10-01 21:27:38
Be aware that the GDI resizing algorithm will produce mediocre results when making major changes in image size. I wrote similar code in the past and then switched to a 3rd party library to get a better quality resizing algorithm.
ScottS
2009-10-01 21:33:49