How to resize image in C#?

Priya
Priya
Member
62 Points
26 Posts

Hi,

How to resize image?

 

Views: 8226
Total Answered: 1
Total Marked As Answer: 1
Posted On: 13-Dec-2014 21:08

Share:   fb twitter linkedin
Answers
Nice One
Nice One
Member
110 Points
10 Posts
         

Hi, Priya

Use following method(it takes three argument and return resized image): 

 
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
 
public Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
 var ratioX = (double)maxWidth / image.Width;
 var ratioY = (double)maxHeight / image.Height;
 var ratio = Math.Min(ratioX, ratioY);
 var newWidth = (int)(image.Width * ratio);
 var newHeight = (int)(image.Height * ratio);
 var newImage = new Bitmap(newWidth, newHeight);
 Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
 return newImage;
}
If you have image byte array then use followin converter methods:

 

public byte[] ImageToByteArray(System.Drawing.Image m_imageIn)
{
 
 MemoryStream oMemoryStream = new MemoryStream();
 // ImageFormat could be other formats like bmp,gif,icon,png etc.
 m_imageIn.Save(oMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
 return oMemoryStream.ToArray();
}
 
 
public System.Drawing.Image ByteArrayToImage(byte[] m_byteArrayIn)
{
 
 MemoryStream oMemoryStream = new MemoryStream(m_byteArrayIn);
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oMemoryStream);
 return oImage;
}

 

Posted On: 13-Dec-2014 21:34
 Log In to Chat