Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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;
        }
 
Share this answer
 
v3
Comments
Suman Zalodiya 19-Sep-11 15:54pm    
It works.

Thanks a lot.
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;
 
Share this answer
 
Why, with extension methods, of course:

C#
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:

C#
Bitmap bmp = Bitmap.FromFile("blah blah");
bmp = bmp.Resize(50);

// or 

bmp = bmp.Resize(320, 240);
 
Share this answer
 
Comments
nsaxena 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 ?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900