PDA

View Full Version : مشکل در تغییر کیفیت عکس در هنگام resize



na3er-faraji
چهارشنبه 19 مهر 1391, 18:04 عصر
سلام. من از کد زیر برای کوچک کردن عکس استفاده میکنم. یک قضیه جالبی که پیش میاد اینه که این کد خود به خود میاد کیفیت عکس رو هم کم میکنه و عکس خیلی بی کیفیت میشه. در حالی که من فقط دارم طول و عرض رو کم میکنم. کسی میدونه علت چیه؟

public static void SaveImage(System.Web.HttpPostedFile postedFile, int max, string fileSaveUrl)
{
System.Drawing.Image thumb = System.Drawing.Image.FromStream(postedFile.InputSt ream, true);
int w = thumb.Width;
int h = (max * thumb.Height) / w;
if (h > max)
{
w = (max * thumb.Width) / thumb.Height;
thumb = thumb.GetThumbnailImage(w, max, new System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback), IntPtr.Zero);
}
else
{
thumb = thumb.GetThumbnailImage(max, h, new System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback), IntPtr.Zero);
}
thumb.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg,System.Dra wing.Imaging.EncoderParameters.);
}

private static bool ThumbnailCallback()
{
return true;
}

clover
پنج شنبه 20 مهر 1391, 13:39 عصر
به نمونه کد های زیر برای تغییر اندازه ی عکس با حفظ کیفیت و همینطور ذخیره ی عکس با کیفیت دلخواه دقت کنید:

تغییر سایز:

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);

Image image = new Bitmap(destWidth, destHeight);
using (Graphics graphics = Graphics.FromImage(image))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
}

return image;
}

دخیره ی عکس JPEG با کیفیت دلخواه:

private static void saveImage(Image image, string path)
{
ImageCodecInfo codecInfo = new List<ImageCodecInfo>(ImageCodecInfo.GetImageEncoders()).Find(x => x.FormatID == ImageFormat.Jpeg.Guid);

// Set the quality
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Qu ality, 80L);

//if (File.Exists(path))
//{
// File.Delete(path);
//}

image.Save(path, codecInfo, parameters);
}

موفق باشید

na3er-faraji
پنج شنبه 20 مهر 1391, 18:08 عصر
ممنون . تابع نهایی من این شد

public static void SaveImage(System.Web.HttpPostedFile postedFile, int max, string fileSaveUrl)
{
System.Drawing.Image thumb = System.Drawing.Image.FromStream(postedFile.InputSt ream, true);
int w = thumb.Width;
int h = (max * thumb.Height) / w;
Graphics g;
Bitmap b;
if (h > max)
{
w = (max * thumb.Width) / thumb.Height;
b = new Bitmap(w, max);
g = Graphics.FromImage((Image)b);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default ;
g.DrawImage(thumb, 0, 0, w, max);
}
else
{
b = new Bitmap(max, h);
g = Graphics.FromImage((Image)b);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default ;
g.DrawImage(thumb, 0, 0, max, h);
}
b.Save(fileSaveUrl,System.Drawing.Imaging.ImageFor mat.Jpeg);
}