PDA

View Full Version : تغییر سایز عکس بدون افت کیفیت



Payman62
شنبه 22 مرداد 1390, 17:15 عصر
سلام.
من از کد زیر برای تغییر سایز عکس استفاده میکنم.


public static Image Resize(Image ImageToResize, Size NewSize)
{
Bitmap MyBitmap = new Bitmap(NewSize.Width, NewSize.Height);
Graphics MyGraphics = Graphics.FromImage((Image)MyBitmap);
MyGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
MyGraphics.DrawImage(ImageToResize, 0, 0, NewSize.Width, NewSize.Height);
MyGraphics.Dispose();
return (Image)MyBitmap;
}

سایز یه عکس 2000 پیکسل در 2000 رو 150 پیکسل در 150 میکنم. یعنی عکس رو کوچیک میکنم. اما پس از تبدیل عکس مات میشه و شفافیتش رو از دست میده. اگه همین تبدیل رو با Paint انجام بدم عکس مات نمیشه. ضمن این که حجم عکس رو هم خیلی بیشتر کاهش میده.
کسی میتونه کمک کنه تا هم عکس مات نشه هم حجمش کمتر شه؟

morteza_carefree
یک شنبه 23 مرداد 1390, 14:56 عصر
من اینو یه چند وقت قبل پیدا کردم اما نتونستم باهاش کار کنم چون ارور داشت
تونستی به منم بگو






public static Bitmap MakeGrayscale2(Bitmap original)
{
unsafe
{
//create an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);

//lock the original bitmap in memory
BitmapData originalData = original.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

//lock the new bitmap in memory
BitmapData newData = newBitmap.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

//set the number of bytes per pixel
int pixelSize = 3;

for (int y = 0; y < original.Height; y++)
{
//get the data from the original image
byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

//get the data from the new image
byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

for (int x = 0; x < original.Width; x++)
{
//create the grayscale version
byte grayScale =
(byte)((oRow[x * pixelSize] * .11) + //B
(oRow[x * pixelSize + 1] * .59) + //G
(oRow[x * pixelSize + 2] * .3)); //R

//set the new image's pixel to the grayscale version
nRow[x * pixelSize] = grayScale; //B
nRow[x * pixelSize + 1] = grayScale; //G
nRow[x * pixelSize + 2] = grayScale; //R
}
}

//unlock the bitmaps
newBitmap.UnlockBits(newData);
original.UnlockBits(originalData);

return newBitmap;
}
}

Payman62
شنبه 05 شهریور 1390, 14:23 عصر
سلام.
دلیل کاهش حجم بیشتر پینت اختلاف کیفیت بود. تو c# هم میشه مثل پینت حجم رو اورد پایین. اما مات مشکل مات شدنش حل نشد.