Click here to Skip to main content
15,885,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to image resize to a user input and want to save it. I use a method to do it. But I don't know to give parameters to my method correct way. I wanna simple code than this

static Image ScaleByPercent(Image imgPhoto, int Percent)
{
    float nPercent = ((float)Percent / 100);

    int sourceWidth = imgPhoto.Width;
    int sourceHeight = imgPhoto.Height;
    int sourceX = 0;
    int sourceY = 0;

    int destX = 0;
    int destY = 0;
    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);

    Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

    Graphics grPhoto = 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;
}
Posted
Updated 11-Aug-11 0:07am
v6

I have tried to clean up the code you posted. The absolute disaster of you posting every last bit of code is one warning I have that you have no idea what you're doing, so I'm not sure if you can understand a clear answer. I can't see exactly what it is you're asking, which bit of code is not working as you expect, and what do you want to do ? I have no idea why you're string mashing names, or why your UI elements have idiotic names. However, I can tell you that to resize an image is much easier than what you're doing, there's a Bitmap constructor that takes a Bitmap and a size for the new Bitmap to create from it.

Edit your post so it shows even less code, and explains what you want us to tell you about the code you end up posting, and what you're trying to do, what it's not doing for you, etc.
 
Share this answer
 
Comments
Christian Graus 11-Aug-11 1:59am    
What is the problem ? You pass in the image and the percent. Did you find this code on the web and you don't understand it ? Like I said, it is overkill, you can use basic math to create a Size and then use the constructor to resize it.

If you can't work out how to call this method, you should not be copying code from the web, or doing anything beyond reading the most basic C# book you can find.
Jeremy Shin Woo 11-Aug-11 2:09am    
How can I explain this. Really I know set parameters to a method. It is a basic thing. Their is two methods. I wanna combine them. I wanna give the image to ScaleByPercent() method from MergeImages() method. there are two images. can u see??
there is an easy method.
Image src_image = Image.FromFile(path);
Image dst_image = src_image.GetThumbnailImage(height, width, null, System.IntPtr.Zero);
dst_image.Savesavename.bmp, System.Drawing.Imaging.ImageFormat.bmp);
 
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