基于C#PictureBox加载显示视觉图像

加载图像

复制代码
 OpenFileDialog file = new OpenFileDialog();
 file.InitialDirectory = ".";
 file.Filter = "所有文件(*.*)|*.*";
 file.ShowDialog();
 if (file.FileName != string.Empty)
 {
     try
     {
         pathname = file.FileName;   //获得文件的绝对路径
         this.pictureBox1.Load(pathname);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 } 

适应窗口

方式1

Picturebox控件SizeMode属性

(1)Normal模式:如果图片大于Picturebox控件大小,图片不能完全显示

(2)AutoSize:自动调整Picturebox控件大小去适应图片的大小,图片可以完全显示。

(3)StretchImage:Picturebox控件大小不变,自动调整图像适应控件。铺满控件

(4)CenterImage:Picturebox控件大小不变,图像从中心开始显示,图片过大会显示不全

(5) Zoom :Picturebox控件大小不变,自动调整图像适应控件。根据宽高显示图像

方式2

复制代码
private void button1_Click(object sender, EventArgs e)
{
    Image image = Image.FromFile(pathname);
    int width = pictureBox1.Width;
    int height = pictureBox1.Height;
    float ratio = (float)width / (float)image.Width;
    
    int newWidth = (int)(image.Width * ratio);
    int newHeight = (int)(image.Height * ratio);
    Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
    bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    Graphics graphic = Graphics.FromImage(bmp);
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
    graphic.Dispose();
    pictureBox1.Image = bmp;
}

保存图像

复制代码
 private void btn_SaveImage_Click(object sender, EventArgs e)
 {
     SaveFileDialog save = new SaveFileDialog();
     save.ShowDialog();
     if (save.FileName != string.Empty)
     {
         pictureBox1.Image.Save(save.FileName);
     } 

 }

读取固定图像路径

复制代码
 private void btn_readImag2_Click(object sender, EventArgs e)
 {
     pathname = "E:\\Halcon数据\\资源图片\\同心度.jpg";
     this.pictureBox1.Load(pathname);
 }
相关推荐
IT_陈寒几秒前
Python 3.12 新特性实战:10个性能优化技巧让你的代码快如闪电⚡
前端·人工智能·后端
听情歌落俗5 分钟前
MATLAB3-2数据存储-台大郭彦甫
开发语言·数学建模·matlab·矩阵
Wiktok16 分钟前
前后端开发Mock作用说明,mock.ts
前端·mock·vue3
冲!!19 分钟前
SCSS 中的Mixins 和 Includes,%是什么意思
前端·css·scss
云天徽上27 分钟前
【数据可视化-112】使用PyEcharts绘制TreeMap(矩形树图)完全指南及电商销售数据TreeMap绘制实战
开发语言·python·信息可视化·数据分析·pyecharts
星语卿1 小时前
Vuetify:构建优雅Vue应用的Material Design组件库
前端·javascript·vue.js
NG WING YIN1 小时前
Golang關於信件的
开发语言·深度学习·golang
Sunny_yiyi2 小时前
Java根据模版导出PDF文件
java·开发语言·pdf