基于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);
 }
相关推荐
阿幸软件杂货间27 分钟前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
通街市密人有29 分钟前
IDF: Iterative Dynamic Filtering Networks for Generalizable Image Denoising
人工智能·深度学习·计算机视觉
蚂蚁RichLab前端团队33 分钟前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
扯淡的闲人37 分钟前
多语言编码Agent解决方案(5)-IntelliJ插件实现
开发语言·python
丑小鸭是白天鹅1 小时前
Kotlin协程详细笔记之切线程和挂起函数
开发语言·笔记·kotlin
孩子 你要相信光1 小时前
css之一个元素可以同时应用多个动画效果
前端·css
sali-tec1 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
java搬砖工-苤-初心不变1 小时前
基于 lua_shared_dict 的本地内存限流实现
开发语言·junit·lua
huangql5201 小时前
npm 发布流程——从创建组件到发布到 npm 仓库
前端·npm·node.js
Days20501 小时前
LeaferJS好用的 Canvas 引擎
前端·开源