Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Windows Application , +
I once again one question about image upload.
 
I want to resize the image before saving at other loadtion.
 
For example i have selected "D:\MemberImage\MyPhoto.jpg" with size 500px*350px.
 
Now before saving this file at other location i want to resize this selected file to 160px*120px and then save this resized image in new location.
 
I mean when i save my file
 
File.Copy(openFileDialog1.filename,@"D:\Images\new.jpg");
 
my new.jpg file should be of size 160px*120px;
 
Please help me some
 
Thanks and regards.
Posted 18-Sep-11 21:57pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Try this for resizing and save to a new location
 
string strPath = @"D:\MemberImage\MyPhoto.jpg";
            System.Drawing.Image img = System.Drawing.Image.FromFile(strPath);
            img=Resize(img, 160, 120);
            img.Save(@"D:\UploadedImages\NewImg.bmp");
 
 
//resize image 
private Image Resize(Image img, int iWidth, int iHeight)
        {
            Bitmap bmp = new Bitmap(iWidth, iHeight);
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.DrawImage(img, 0, 0, iWidth, iHeight);
           
            return (Image)bmp;
        }
  Permalink  
v3
Comments
suman161288 at 19-Sep-11 15:54pm
   
It works.
 
Thanks a lot.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

try the following :
string directory = AppDomain.CurrentDomain.BaseDirectory + "PhotoGallery/Slides/";
                        Bitmap originalBMP = new Bitmap(fuphotos.FileContent);
 
                        int origWidth = originalBMP.Width;
                        int origHeight = originalBMP.Height;
                        int sngRatio = origWidth / origHeight;
                        int newWidth = 213;
                        int newHeight = 200;
 
                        Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
 
                        Graphics oGraphics = Graphics.FromImage(newBMP);
                        oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
                        oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                        newBMP.Save(directory + cfilename);
 
                        originalBMP.Dispose();
                        newBMP.Dispose();
                        oGraphics.Dispose();
 
                        strcFilepath = @"~/PhotoGallery/Slides/" + cfilename;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Why, with extension methods, of course:
 
public static class ExtendBitmap
{
    public static Bitmap Resize(this Bitmap bmp, int nWidth, int nHeight )
    {
        Bitmap result = new Bitmap(nWidth, nHeight);
        using(Graphics g = Graphics.FromImage((Image)result))
        {
            g.DrawImage(bmp, 0, 0, nWidth, nHeight);
        }
        return result;
    }	
 
    public static Bitmap Resize(this Bitmap bmp, double percent)
    {
        int width = (int)(bmp.Width * (percent*0.01));
        int height = (int)(bmp.Height * (percent*0.01));
        Bitmap result = bmp.Resize(width, height);
        return result;
    }	
}
 
Load the bitmapo the way you normally would, and then do this:
 
Bitmap bmp = Bitmap.FromFile("blah blah");
bmp = bmp.Resize(50);
 
// or 

bmp = bmp.Resize(320, 240);
  Permalink  
Comments
nsaxena at 21-Dec-12 5:59am
   
Whatever you do. You are going to loose transparency, it will be replaced with black color.
 
Any solution for bitmaps ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 30 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100