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 Maciej Los 184
1 Sascha Lefévre 120
2 Abhinav S 104
3 Sergey Alexandrovich Kryukov 94
4 Richard MacCutchan 90
0 Sergey Alexandrovich Kryukov 6,628
1 OriginalGriff 6,298
2 Peter Leow 2,534
3 Maciej Los 2,492
4 Abhinav S 2,378


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 30 Aug 2012
Copyright © CodeProject, 1999-2015
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