Reputation: 1727
POST EDITED ADDED THE LINK
Read a very good post on image resize [here][1] in asp.net mvc.
http://dotnetslackers.com/articles/aspnet/Testing-Inbound-Routes.aspx
I need this logic to work for the images that are uploaded in cdn also.Say for example i have uploaded an image in cdn and now i want to fetch it from my controller and resize it.Also the image should not be saved in my server as it will not be good idea as it consumes valuable resource.The image has to be read from CDN and re sized without saving it locally in server.How can we achieve this using the methodology given in the above post.
Thanks, S.
Upvotes: 2
Views: 2367
Reputation: 91
If you use ASP.Net MVC3, you can try new helper - WebImage.
This is my test code.
public ActionResult GetImg(float rate)
{
WebClient client = new WebClient();
byte[] imgContent = client.DownloadData("ImgUrl");
WebImage img = new WebImage(imgContent);
img.Resize((int)(img.Width * rate), (int)(img.Height * rate));
img.Write();
return null;
}
Upvotes: 9
Reputation: 8214
Here's what I use. Works great.
private static Image ResizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
Upvotes: 1
Reputation: 21
You can use the GDI+ features in the System.Drawing namespace
Bitmap newBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)newBitmap);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, 0, 0, destWidth, destHeight);
g.Dispose();
Upvotes: 1