I am resizing image in asp.net.I succeeded resizing image.but while converting it as stream .jpg images are not working.
here my code if i set image format as jpeg it is not working.Because in C# there is no image format for .jpg
public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
var width=image.Width;
var height=image.Height;
var newWidth=0;
var newHeight=0;
var divisor=0;
if (width > height) {
newWidth = maxWidth;
divisor = width / maxWidth;
if (divisor == 0)
{
divisor = 1;
}
newHeight = Convert.ToInt32(height /divisor);
}
else {
newHeight = maxHeight;
divisor = height / maxHeight;
if (divisor == 0)
{
divisor = 1;
}
newWidth = Convert.ToInt32(width / divisor);
}
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
public static Stream ToStream(this System.Drawing.Image image, ImageFormat formaw)
{
var stream = new System.IO.MemoryStream();
//stream.ReadTimeout = 100000;
image.Save(stream, formaw);
stream.Position = 0;
//stream.ReadTimeout = 100000;
return stream;
}