PDA

View Full Version : حرفه ای: تبدیل یک عکس بزرگ به کوچک



morteza_carefree
یک شنبه 09 مرداد 1390, 19:23 عصر
سللم دوستان
من از panel توی برنامه عکس میگیرم و توی یک bitmap ذخیره میکنم و میخوام که اون رو به یه عکس کوچکتر که اندازش رو کاربر وارد میکنه تبدیل کنم اما هر کاری میکنم درست در نمیاد
کدهامو میزارم میشه کمکم کنید



private void pic2pix(Bitmap org)
{
int height = int.Parse(h.Text);// ورودی تکس باک برای ارتفاع عکس
int width = int.Parse(w.Text);//عرض عکس
int h2, w2;

h2 = org.Height / height;
w2 = org.Width / width;
int tj, ti;
Bitmap bmp = new Bitmap(width, height);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
ti = i + (w2 / 2);
tj = j + (h2 / 2);
Color clr = org.GetPixel(ti,tj);
bmp.SetPixel(i, j, clr);
}
}

pictureBox2.Image = bmp;
}

mmd2009
یک شنبه 09 مرداد 1390, 23:25 عصر
با سلام

دوست عزیز این یک متد خوب برای این کار :


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

Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();

return (Image)b;
}


لینک اصلی کد (http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing)

morteza_carefree
چهارشنبه 12 مرداد 1390, 15:26 عصر
دوست عزیز من از کدی که دادید استفاده کردم اما این کد وقتی عکس زیاد کوچیک میشه رنگ هارو قاطی میکنه کد دیگه ای نیست