private static System.Drawing.Image RezizeImage(System.Drawing.Image img, int maxWidth, int maxHeight)
{
if (img.Height < maxHeight && img.Width < maxWidth) return img;
using (img)
{
Double xRatio = (double)img.Width / maxWidth;
Double yRatio = (double)img.Height / maxHeight;
Double ratio = Math.Max(xRatio, yRatio);
int nnx = (int)Math.Floor(img.Width / ratio);
int nny = (int)Math.Floor(img.Height / ratio);
Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage(cpy))
{
gr.Clear(Color.Transparent);
// This is said to give best quality when resizing images
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage(img,
new Rectangle(0, 0, nnx, nny),
new Rectangle(0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
private static MemoryStream BytearrayToStream(byte[] arr)
{
return new MemoryStream(arr, 0, arr.Length);
}
public static void HandleImageUpload(string p_postedImageFileName, int width, int height, Stream toStream, string path)
{
FileStream fs = default(FileStream);
string originalFileName = HttpContext.Current.Server.MapPath(path + p_postedImageFileName.Replace(" ", "").Replace("%20", ""));
fs = new FileStream(originalFileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] binaryImage = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
System.Drawing.Image img = RezizeImage(System.Drawing.Image.FromStream(BytearrayToStream(binaryImage)), width, height);
img.Save(toStream, System.Drawing.Imaging.ImageFormat.Png);
}
{
if (img.Height < maxHeight && img.Width < maxWidth) return img;
using (img)
{
Double xRatio = (double)img.Width / maxWidth;
Double yRatio = (double)img.Height / maxHeight;
Double ratio = Math.Max(xRatio, yRatio);
int nnx = (int)Math.Floor(img.Width / ratio);
int nny = (int)Math.Floor(img.Height / ratio);
Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage(cpy))
{
gr.Clear(Color.Transparent);
// This is said to give best quality when resizing images
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage(img,
new Rectangle(0, 0, nnx, nny),
new Rectangle(0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
private static MemoryStream BytearrayToStream(byte[] arr)
{
return new MemoryStream(arr, 0, arr.Length);
}
public static void HandleImageUpload(string p_postedImageFileName, int width, int height, Stream toStream, string path)
{
FileStream fs = default(FileStream);
string originalFileName = HttpContext.Current.Server.MapPath(path + p_postedImageFileName.Replace(" ", "").Replace("%20", ""));
fs = new FileStream(originalFileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] binaryImage = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
System.Drawing.Image img = RezizeImage(System.Drawing.Image.FromStream(BytearrayToStream(binaryImage)), width, height);
img.Save(toStream, System.Drawing.Imaging.ImageFormat.Png);
}
No comments:
Post a Comment