Saving as jpeg from memorystream in c#
c#, image, jpeg, memorystream
Solution
Resize the Image and Save it
Private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
double srcWidth = img.Source.Width;
double srcHeight = img.Source.Height;
double resizeWidth = srcWidth;
double resizeHeight = srcHeight;
double aspect = resizeWidth / resizeHeight;
if (resizeWidth > maxWidth)
{
resizeWidth = maxWidth;
resizeHeight = resizeWidth / aspect;
}
if (resizeHeight > maxHeight)
{
aspect = resizeWidth / resizeHeight;
resizeHeight = maxHeight;
resizeWidth = resizeHeight * aspect;
}
img.Width = resizeWidth;
img.Height = resizeHeight;
}
You could use this code to Resize the image to the required Dimention Before saving it
Problem
I have a method as shown below to save image as jpeg. I want to save all the pictures with the same height and width without it getting distorted. How can I do that? Please help ``` public void SaveFileOnDisk(MemoryStream ms, string FileName) { try { string appPath = HttpContext.Current.Request.ApplicationPath; string physicalPath = HttpContext.Current.Request.MapPath(appPath); string strpath = physicalPath + "\\Images"; string WorkingDirectory = strpath; System.Drawing.Image imgSave = System.Drawing.Image.FromStream(ms); Bitmap bmSave = new Bitmap(imgSave); Bitmap bmTemp = new Bitmap(bmSave); Graphics grSave = Graphics.FromImage(bmTemp); grSave.DrawImage(imgSave, 0, 0, imgSave.Width, imgSave.Height); bmTemp.Save(WorkingDirectory + "\\" + FileName + ".jpg"); imgSave.Dispose(); bmSave.Dispose(); bmTemp.Dispose(); grSave.Dispose(); } catch (Exception ex) { //lblMsg.Text = "Please try again later."; } } ```