views:

477

answers:

5

Hello!

How can I resize an image on server that I just uploaded? I using C# with .NET Framework 3.5 SP1.

Thanks!

A: 

see this answer

RC
He wants to resize the image and not to check the dimension.
rahul
He can resize with GD too !
RC
Is this actually an answer? Why not just a comment? Or a dup?
Marc Gravell
will comment next time, thanks for the advice.
RC
A: 

Plz check this thread and get code from there from my answer.

http://stackoverflow.com/questions/1230090/create-a-new-image-on-the-file-system-from-system-drawing-image/1233199#1233199

Code:

System.Drawing.Bitmap bmpOut = new System.Drawing.Bitmap(NewWidth, NewHeight);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpOut);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, NewWidth, NewHeight);
    g.DrawImage(new System.Drawing.Bitmap(fupProduct.PostedFile.InputStream), 0, 0, NewWidth, NewHeight);
    MemoryStream stream = new MemoryStream();
    switch (fupProduct.FileName.Substring(fupProduct.FileName.IndexOf('.') + 1).ToLower())
    {
        case "jpg":
            bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            break;
        case "jpeg":
            bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            break;
        case "tiff":
            bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Tiff);
            break;
        case "png":
            bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            break;
        case "gif":
            bmpOut.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
            break;
    }
    String saveImagePath = Server.MapPath("../") + "Images/Thumbnail/" + fupProduct.FileName.Substring(fupProduct.FileName.IndexOf('.'));
    bmpOut.Save(saveImagePath);
Muhammad Akhtar
If this seems to be a dup then point this as a comment.
rahul
jpg" and "jpeg" cases are duplicated. you can case "jpg": case "jpeg": bmpOut.Save(... break;
Ken Egozi
Note importantly (http://msdn.microsoft.com/en-us/library/system.drawing.aspx) > Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service.
Marc Gravell
@Marc Thanks.....
Muhammad Akhtar
+1  A: 

Have you tried this?

public Image resize( Image img, int width, int height )
    {
        Bitmap b = new Bitmap( width, height ) ;
        Graphics g = Graphics.FromImage( (Image ) b ) ;
 g.DrawImage( img, 0, 0, width, height ) ;
    g.Dispose() ;

    return (Image ) b ;
}
daxsorbito
Note importantly (http://msdn.microsoft.com/en-us/library/system.drawing.aspx) > Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service.
Marc Gravell
A: 

the snippet I always use:

var target = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
target.SetResolution(source.HorizontalResolution,
source.VerticalResolution);

using (var graphics = Graphics.FromImage(target))
{
    graphics.Clear(Color.White);
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

    graphics.DrawImage(source,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, source.Width, source.Height),
        GraphicsUnit.Pixel);
}

return target;

Ken Egozi
Note importantly (http://msdn.microsoft.com/en-us/library/system.drawing.aspx) > Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service.
Marc Gravell
A: 

Try the following method:

 public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath)
    {
        System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
        double widthRatio = (double)fullSizeImg.Width / (double)Width;
        double heightRatio = (double)fullSizeImg.Height / (double)Height;
        double ratio = Math.Max(widthRatio, heightRatio);
        int newWidth = (int)(fullSizeImg.Width / ratio);
        int newHeight = (int)(fullSizeImg.Height / ratio);
        //System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        //DateTime MyDate = DateTime.Now;
        //String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf("."));
        thumbNailImg.Save(destPath, ImageFormat.Jpeg);
        thumbNailImg.Dispose();
        return "";
    }
    public bool ThumbnailCallback() { return false; }
Himadri
And file size? I want to reduce file size also.
VansFannel