图片适应pictureBox大小显示代码
2013-01-10 14:22
239 查看
#region 在图片框中显示相应大小的图片
private Bitmap ResizeImage(Bitmap bmp, PictureBox picBox)
{
float xRate = (float)bmp.Width / picBox.Size.Width;//比较picBox的宽度与图片本身的宽度
float yRate = (float)bmp.Height / picBox.Size.Height;
if (xRate <= 1 && yRate <= 1)//图片比picBox小
{
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();
}
}
}
}
#endregion
#region 上传本地图片并预览
private void btn_preview_Click(object sender, EventArgs e)
{
OpenFileDialog ofdlg = new OpenFileDialog();
ofdlg.Title = "选择上传的图片";
ofdlg.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
ofdlg.ShowDialog();//显示文件对话框
txt_ImgPath.Text = ofdlg.FileName;//获取文件名并填充在文件路径文本框
if (File.Exists(ofdlg.FileName))//判断文件是否存在
{
Bitmap bmp = new Bitmap(ofdlg.FileName);
Bitmap Pic = ResizeImage(bmp,pictureBox_nowpic);//调用调整大小函数
pictureBox_nowpic.Image = Pic;
}
else
{
MessageBox.Show(this, "图片为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
#endregion
备注:
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
设置图片与 g 关联的插补模式为InterpolationMode.HighQualityBicubic(指定高质量的双线性插值法。执行预筛选以确保高质量的收缩)。
private Bitmap ResizeImage(Bitmap bmp, PictureBox picBox)
{
float xRate = (float)bmp.Width / picBox.Size.Width;//比较picBox的宽度与图片本身的宽度
float yRate = (float)bmp.Height / picBox.Size.Height;
if (xRate <= 1 && yRate <= 1)//图片比picBox小
{
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();
}
}
}
}
#endregion
#region 上传本地图片并预览
private void btn_preview_Click(object sender, EventArgs e)
{
OpenFileDialog ofdlg = new OpenFileDialog();
ofdlg.Title = "选择上传的图片";
ofdlg.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
ofdlg.ShowDialog();//显示文件对话框
txt_ImgPath.Text = ofdlg.FileName;//获取文件名并填充在文件路径文本框
if (File.Exists(ofdlg.FileName))//判断文件是否存在
{
Bitmap bmp = new Bitmap(ofdlg.FileName);
Bitmap Pic = ResizeImage(bmp,pictureBox_nowpic);//调用调整大小函数
pictureBox_nowpic.Image = Pic;
}
else
{
MessageBox.Show(this, "图片为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
#endregion
备注:
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
设置图片与 g 关联的插补模式为InterpolationMode.HighQualityBicubic(指定高质量的双线性插值法。执行预筛选以确保高质量的收缩)。
相关文章推荐
- 背景图片自适应浏览器分辨率大小并自动拉伸全屏代码
- 同时显示图片和文字,重绘QPushButton,让button大小适应图片大小
- 网页背景图片自动适应窗口大小的代码
- 用CSS来控制图片显示大小的代码
- 在JLabel上显示图片,并且图片自适应JLabel的大小
- 在Picture Control控件上显示JPG图片并让图片适应控件的大小
- 用css来控制图片大小显示的实现方法与代码
- jQuery动态改变图片显示大小(修改版)的实现思路及代码
- 让PictureBox自动适应图片的大小
- jQuery动态改变图片显示大小(修改版)的实现思路及代码
- 应用键横竖屏切换;label中显示图片;不同类型设备适配的代码;UIWebView字体大小、字体颜色、背景色的设置;
- C#的PictureBox显示图片与实际图片大小不符的解决办法
- 图片自动适应大小jquery代码
- 网页背景图片自动适应窗口大小的代码
- 用css来控制图片大小显示的实现方法与代码
- [转]在JLabel上显示图片,并且图片自适应JLabel的大小
- 当picturebox的显示图片大小为0K时
- 网页背景图片自动适应窗口大小的代码
- jQuery动态改变图片显示大小(修改版)的实现思路及代码
- jQuery动态改变图片显示大小(修改版)的实现思路及代码