基于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);
 }
相关推荐
持梦远方2 分钟前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++
abigale037 分钟前
webpack+vite前端构建工具 -11实战中的配置技巧
前端·webpack·node.js
zzywxc78719 分钟前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
专注API从业者26 分钟前
构建淘宝评论监控系统:API 接口开发与实时数据采集教程
大数据·前端·数据库·oracle
Joker`s smile30 分钟前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome
weixin_4166399732 分钟前
爬虫工程师Chrome开发者工具简单介绍
前端·chrome·爬虫
HuashuiMu花水木34 分钟前
Matplotlib笔记4----------图像处理
图像处理·笔记·matplotlib
我是如子啊38 分钟前
【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
前端·chrome·edge
灵性花火38 分钟前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
DES 仿真实践家2 小时前
【Day 11-N22】Python类(3)——Python的继承性、多继承、方法重写
开发语言·笔记·python