基于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);
 }
相关推荐
汉得数字平台1 分钟前
【鲲苍提效】全面洞察用户体验,助力打造高性能前端应用
前端·前端监控
向宇it3 分钟前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
花海如潮淹8 分钟前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux
_丿丨丨_5 小时前
XSS(跨站脚本攻击)
前端·网络·xss
古月-一个C++方向的小白5 小时前
C++11之lambda表达式与包装器
开发语言·c++
天天进步20155 小时前
前端安全指南:防御XSS与CSRF攻击
前端·安全·xss
沐知全栈开发6 小时前
Eclipse 生成 jar 包
开发语言
杭州杭州杭州7 小时前
Python笔记
开发语言·笔记·python
tanyongxi667 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
拾光拾趣录8 小时前
括号生成算法
前端·算法