jpg文件适合picturebox大小显示代码

private Bitmap JpgToBmp()
{
Bitmap img1 = new Bitmap(Width, Height);
Graphics g1 = Graphics.FromImage(img1);
Image img2 = Image.FromFile(@"D:\gisc#\\P11-06-11_08.49.jpg");
g1.DrawImage(img2, new Rectangle(Point.Empty, img2.Size));
return img1;
}

private Bitmap ResizeImage(Bitmap bmp, PictureBox picBox)
{
float xRate = (float)bmp.Width / picBox.Size.Width;
float yRate = (float)bmp.Height / picBox.Size.Height;
if (xRate <= 1 && yRate <= 1)
{
return bmp;
}
else
{
float tRate = (xRate >= yRate) ? xRate : yRate;
Graphics g = null;
try
{
int newW = (int)(bmp.Width / tRate);
int newH = (int)(bmp.Height / tRate);
Bitmap b = new Bitmap(newW, newH);
g = Graphics.FromImage(b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
//bmp.Dispose();
return b;
}
catch
{
//bmp.Dispose();
return null;
}
finally
{
if (null != g)
{
g.Dispose();
}
}
}
}

posted @ 2013-05-15 17:54  之远  阅读(153)  评论(0编辑  收藏  举报