基于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);
 }
相关推荐
李奶酪10 分钟前
WebSocket相关技术
前端
m0_7482417015 分钟前
rust web框架actix和axum比较
前端·人工智能·rust
心灵宝贝32 分钟前
用DeepSeek生成批量删除处理 PDF第一页工具
开发语言·microsoft·c#
goomind1 小时前
YOLOv8车牌关键点定位与矫正识别系统
深度学习·yolo·计算机视觉·车牌识别·车牌定位·车牌关键点
努力的小帅1 小时前
c/c++内存管理
开发语言·c++
AC-PEACE1 小时前
React 项目创建与文件基础结构关系
前端·javascript·react.js
IT、木易1 小时前
白话React第九章React 前沿技术与企业级应用实战
前端·react.js·前端框架
xuxiaoxie1 小时前
安装electron 提示RequestError: certificate has expired
前端·javascript·electron
ceffans1 小时前
PDF文档中表格以及形状解析
开发语言·c++·windows·pdf
皮克斯的进化之路1 小时前
Java中常见的设计模式
java·开发语言·设计模式