views:

242

answers:

8
+1  Q: 

Resizing images

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
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
A: 

load it into gdi+(system.drawing) and scale?

UK-AL
A: 

Check this one

Simple Image Resizing Library

rahul
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
-1: num1, num2, num3, ... num11. No offense, but the choice of variable names reads like the code is purposefully obfuscated.
Juliet
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
Kidooosss... this code is from .NET Reflector. I have the idea of copy-paste. Intention is to show only logic. rating... your choice. :-)
NinethSense
+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
A: 

Since nobody else has said this: try WPF.

MusiGenesis
+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
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