Last post Feb 23, 2011 02:55 PM by oneuser
Feb 22, 2011 10:23 AM|oneuser|LINK
I am trying to resize an image to 400 * 400 pixels.
When I save the image the image is not resizing. The size remains the same as of original code.
could you please check what is wrong with the code.
The original image size is 300 * 200 pixels.
using (WebClient c = new WebClient())
using (MemoryStream ms = new MemoryStream(c.DownloadData("http://test12345.com/testimage.jpg")))
System.Drawing.Image i = System.Drawing.Image.FromStream(ms);
Graphics graphic = Graphics.FromImage(i);
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
graphic.DrawImage(i, 0, 0, 400, 400);
Feb 22, 2011 01:11 PM|AceCorban|LINK
I have this method which I use pretty extensively for creating resized images. It also keeps the resolution the same as the original:
protected bool ResizeAbort()
protected void CreateResizedCopy(String sourcePath, int maxWidth, int maxHeight)
System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(sourcePath));
int width = fullSizeImg.Width;
int height = fullSizeImg.Height;
if ((maxWidth > 0) && (width > maxWidth))
double resolution = (height * 1.0) / (width * 1.0);
width = maxWidth;
height = Convert.ToInt32(Math.Floor(resolution * width));
else if ((maxHeight > 0) && (height > maxHeight))
double resolution = (width * 1.0) / (height * 1.0);
height = maxHeight;
width = Convert.ToInt32(Math.Floor(resolution * height));
System.Drawing.Image resizedImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallback, IntPtr.Zero);
String filename = sourcePath.Substring(sourcePath.LastIndexOf("/")+1);
width = Convert.ToInt32(Math.Floor(width * 0.25));
height = Convert.ToInt32(Math.Floor(height * 0.25));
Feb 22, 2011 06:42 PM|oneuser|LINK
Even with your code the images are not getting resized. May be I am doing something wrong.
Could you please check my code and if you can see some issue.
Feb 23, 2011 02:01 PM|AceCorban|LINK
Yeah, I'm sorry, I just realized, mine probably won't work because it is used to make smaller images, not larger ones. So you want to stretch a 300X200 into a 400X400? Visual issues aside, why don't you just let HTML do that?
<img src="my300by200Img.jpg" style="width:400px; height:400px;"/>
Typically people's problem is that they don't want to rely on this method to show smaller images because the client still has to download the full size image, even if HTML is squashing it. But if you want to stretch it, this is best for both the user and
you because you can store a smaller image on your webserver, thereby taking up less space.
Feb 23, 2011 02:04 PM|oneuser|LINK
Thanks but that is not feasible in my case as we need to keep the quality of the images as well.
I just need to push images to the server through a service without changing the existing front end code.
Feb 23, 2011 02:21 PM|AceCorban|LINK
I honestly don't know that you can increase the size of an image while keeping its "crispness". Even photoshop has a tough time doing that. There are some fundamental issues with your problem. By changing the resolution and increasing size, you are going
to have quality issues with the final image.
Feb 23, 2011 02:55 PM|oneuser|LINK
But thanks anyways.
It is now finally working. after changing the size of the source in code.