基于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);
 }
相关推荐
芳草萋萋鹦鹉洲哦36 分钟前
【tauri+pixijs】关于unicode/ascII/GB2312
前端·tauri·pixijs
月屯36 分钟前
后端go完成文档分享链接功能
开发语言·后端·golang
木易 士心43 分钟前
th-table 中 基于双字段计算的表格列展示方案
前端·javascript·angular.js
Franciz小测测1 小时前
Python连接RabbitMQ三大方案全解析
开发语言·后端·ruby
代码雕刻家1 小时前
C语言的左对齐符号-
c语言·开发语言
小肖爱笑不爱笑1 小时前
2025/11/19 网络编程
java·运维·服务器·开发语言·计算机网络
fakaifa1 小时前
【全开源】智慧共享农场源码独立版+uniapp前端
前端·uni-app·智慧农场·源码下载·智慧农场小程序·智慧共享农场
AI浩1 小时前
Cambrian-S:迈向视频中的空间超感知
人工智能·目标检测·计算机视觉·音视频
toooooop82 小时前
uniapp多个页面监听?全局监听uni.$emit/$on
前端·javascript·uni-app
骨子里的偏爱2 小时前
【案例】uniapp实现内部信息与外部的html网页双向通信的完整的过程,附加完整的代码部分
前端·uni-app·html