目录

前言

一、界面设计

二、关键技术

1.把图片拖入到窗体并显示

2.实现图像缩放的功能

3.实现图像的移动效果

4.实时显示当前鼠标处的RGB值

5. 右击功能的实现

6.效果展示

总结

前言

使用C#制作一个图片查看器,具有滚轮放大缩小,鼠标拖动,图像像素化,显示颜色RGB信息功能。


一、界面设计

窗体中需要添加panel,statusStrip,contextMenuStrip,timer等控件,如图所示。

二、关键技术

1.把图片拖入到窗体并显示

代码如下:

  private void Form1_DragEnter(object sender, DragEventArgs e){SetDragImageToFrm(this, e);}Point imagePoint = new Point();Bitmap bkImage;//定义Bitmap变量/// <summary>/// 在窗体背景中显示被拖放的图片/// </summary>public void SetDragImageToFrm(Form form, DragEventArgs e){e.Effect = DragDropEffects.Copy;//设置拖放操作中目标放置类型为复制//检索数据格式相关联的数据String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);string strPath;strPath = str_Drop[0];//获取拖放文件的目录try{bkImage = new Bitmap(strPath);//存储拖放的图片pictureBox1.Image = bkImage;//在pictureBox上显示图片}catch { }}

效果展示如下:更详细的介绍请参考以下文章:

http://t.csdn.cn/gf178

2.实现图像缩放的功能

需要把pictureBox的SizeMode属性设为Zoom,如图所示:

需要在Form.Designer.cs界面中添加MouseWheel事件,如图所示:

然后添加pictureBox1_MouseWheel函数,代码如下:

 public void pictureBox1_MouseWheel(object sender, MouseEventArgs e){int x = e.Location.X;int y = e.Location.Y;int ow = pictureBox1.Width;int oh = pictureBox1.Height;int VX, VY;  //因缩放产生的位移矢量if (e.Delta > 0) //放大{int width = Convert.ToInt32(pictureBox1.Width * 1.5);int height = Convert.ToInt32(pictureBox1.Height * 1.5);if (width * height > 45800000)return;pictureBox1.Width = width;pictureBox1.Height = height;PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);pictureBox1.Width = rect.Width;pictureBox1.Height = rect.Height;}if (e.Delta < 0) //缩小{//防止一直缩成负值if (pictureBox1.Width < 100 || pictureBox1.Height < 100)return;pictureBox1.Width = pictureBox1.Width / 2;pictureBox1.Height = pictureBox1.Height / 2;PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);pictureBox1.Width = rect.Width;pictureBox1.Height = rect.Height;}//求因缩放产生的位移,进行补偿,实现锚点缩放的效果VX = (int)((double)x * (ow - pictureBox1.Width) / ow);VY = (int)((double)y * (oh - pictureBox1.Height) / oh);pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);}

3.实现图像的移动效果

需要把pictureBox的事件栏中添加MouseDown,MouseMove,MouseUp事件,如图所示:

事件代码如下:

        Point mouseDownPoint = new Point();bool flagIsMove = false;private void pictureBox1_MouseDown(object sender, MouseEventArgs e){if(e.Button==MouseButtons.Left){mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置mouseDownPoint.Y = Cursor.Position.Y;flagIsMove = true;pictureBox1.Focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点}}private void pictureBox1_MouseMove(object sender, MouseEventArgs e){pictureBox1.Focus(); //鼠标在picturebox上时才有焦点,此时可以缩放if (flagIsMove){int x, y;   //新的pictureBox1.Location(x,y)int moveX, moveY; //X方向,Y方向移动大小。moveX = Cursor.Position.X - mouseDownPoint.X;moveY = Cursor.Position.Y - mouseDownPoint.Y;x = pictureBox1.Location.X + moveX;y = pictureBox1.Location.Y + moveY;pictureBox1.Location = new Point(x, y);mouseDownPoint.X = Cursor.Position.X;mouseDownPoint.Y = Cursor.Position.Y;}}private void pictureBox1_MouseUp(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){flagIsMove = false;}}

4.实时显示当前鼠标处的RGB值

添加一个timer工具,并添加代码如下:

  private void timer1_Tick(object sender, EventArgs e){Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y);//获取鼠标的点位信息Color cl = GetColor(pt);//获取鼠标在当前位置的颜色toolStripStatusLabelRGB.Text = "(R,G,B):(" + cl.R + "," + cl.G + "," + cl.B + ")";//获取当前颜色的RGB值}[DllImport("gdi32.dll")]static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);[DllImport("gdi32.dll")]static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);public Color GetColor(Point screenPoint){IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero);uint colorref = GetPixel(displayDC, screenPoint.X, screenPoint.Y);byte Red = GetRValue(colorref);byte Green = GetGValue(colorref);byte Blue = GetBValue(colorref);return Color.FromArgb(Red, Green, Blue);}static public byte GetRValue(uint color){return (byte)color;}static public byte GetGValue(uint color){return ((byte)(((short)(color)) >> 8));}static public byte GetBValue(uint color){return ((byte)((color) >> 16));}static public byte GetAValue(uint color){return ((byte)((color) >> 24));}

5. 右击功能的实现

添加一个contextMenuStrip工具,并在Form1属性栏中ContextMenuStrip中添加ContextMenuStrip1,如下图所示:

添加像素化和图像自适应功能,如下图所示:

并添加相应代码,如下:

   private void 像素化ToolStripMenuItem_Click(object sender, EventArgs e){Bitmap bitmap;bitmap = Magnifier(bkImage, 2);pictureBox1.Image = bitmap;}/// <summary>/// 放大图片,使图片像素化/// </summary>/// <param name="srcbitmap"></param>/// <param name="multiple"></param>/// <returns></returns>public Bitmap Magnifier(Bitmap srcbitmap, int multiple){if (multiple <= 0) { multiple = 0; return srcbitmap; }Bitmap bitmap = new Bitmap(srcbitmap.Size.Width * multiple, srcbitmap.Size.Height * multiple);BitmapData srcbitmapdata = srcbitmap.LockBits(new Rectangle(new Point(0, 0), srcbitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);BitmapData bitmapdata = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);unsafe{byte* srcbyte = (byte*)(srcbitmapdata.Scan0.ToPointer());byte* sourcebyte = (byte*)(bitmapdata.Scan0.ToPointer());for (int y = 0; y < bitmapdata.Height; y++){for (int x = 0; x < bitmapdata.Width; x++){long index = (x / multiple) * 4 + (y / multiple) * srcbitmapdata.Stride;sourcebyte[0] = srcbyte[index];sourcebyte[1] = srcbyte[index + 1];sourcebyte[2] = srcbyte[index + 2];sourcebyte[3] = srcbyte[index + 3];sourcebyte += 4;}}}srcbitmap.UnlockBits(srcbitmapdata);bitmap.UnlockBits(bitmapdata);return bitmap;}private void 图像自适应ToolStripMenuItem_Click(object sender, EventArgs e){pictureBox1.Location = panel1.Location;pictureBox1.Width = panel1.Width;pictureBox1.Height = panel1.Height;pictureBox1.Image = bkImage;//在pictureBox上显示图片}

6.效果展示


总结

成功制作了一个功能丰富的图片查看器。

C#制作一个图片查看器,具有滚轮放大缩小,鼠标拖动,图像像素化,显示颜色RGB信息功能相关推荐

  1. 前端实现图片查看器(滚轮放大,缩小,旋转)——Viewer.js

    一.前端实现图片查看器(放大,缩小,旋转)--Viewer.js [附参考链接:(14条消息) Viewer.js第一次点击图片无法显示解决方案(vue使用)_李墨瞳的博客-CSDN博客] 1.0 J ...

  2. vue图片查看器,支持放大 缩小 还原

    Vue中使用图片查看器 如何使用 项目中使用的是 vue + element-ui 要求查看图片时能够实现 图片放大缩小翻转等功能,于是找到一个适合的插件,首先看下效果: 功能与我们设想的一样,下面看 ...

  3. java swing awt绘制一个图片查看器 图片显示 图片控件

    感谢 java图片查看器 的代码 java似乎没有一个名字叫图片控件的 控件,使用swing 的Label显示图片 他的代码如下: package swing.draw; import java.aw ...

  4. 前端图片拖拽,滚轮放大缩小

    拖拽 var offX = 0,offY = 0; onMouseDown(ev) {var svgId = document.getElementById("svgId");sv ...

  5. python画生日_使用PYTHON制作一个生日查看器

    python中的字典说白了就是一个键值对,birthdays = {'张三': '12月1日', '李四并': '11月23日', '普通': '6月2日'} 通过键取得值的内容,例如birthday ...

  6. QT制作一个图片播放器

    前言:使用qt制作了一个简单的图片播放器,可以播放gif.png等格式图片 先来看看播放器的功能(当然是很简陋的,没有很深入的设计): 1.点击图片列表中图片进行播放. 2.自动播放,播放的图片的间隔 ...

  7. js实现图片在div内滚轮放大缩小,有滚动条,双击回原状

    效果图: <style type="text/css"> *{padding:0;margin:0;} img{border:none;} #display{max-w ...

  8. 【Swing】图片查看器

    实现一个图片查看器: -加载文件夹里的图片显示 -显示缩略图 -点击缩略图时显示大图 -当前图片高亮显示 package my;import java.awt.Container; import ja ...

  9. [分享]牛牛图片查看器[仿QQ图片查看器]及大致原理说明

    这是一个完全通过Duilib来实现的一个图片查看器,仿照QQ图片查看器的效果实现,希望大家喜欢. 先看一下使用的截图吧: 做相关说明如下: 一:使用说明 1. 通过命令行调用,传递图片路径即可打开图片 ...

最新文章

  1. 网络工程师_记录的一些真题_2018上半年上午
  2. 中文语言能力评测基准「智源指数」问世:覆盖17种主流任务,19个代表性数据集,更全面、更均衡...
  3. wpf控件设计时支持(1)
  4. Lightgbm with Hyperopt
  5. php视频文件上传到服务器,上传和下载图片和视频到服务器
  6. 华师大c语言作业答案,2018华东师范大学计算机系机试题目代码
  7. 系列(七)—测试用例设计
  8. 使用Hibernate-Validator优雅的验证RESTful Web Services的参数
  9. 33 个送给 Java 程序员的练手项目合集
  10. 前端每隔几秒发送一个请求
  11. Js对象如何添加方法、查看Api
  12. 彻底安装oracle数据库,安装 Oracle 数据库软件
  13. Context 使用不当造成内存泄露
  14. sd卡驱动 android,sd卡驱动异常怎么办 sd卡驱动程序无法使用【详解】
  15. ICCV2019 | 锁定视频中的目标:港大提出运动注意力检测方法
  16. 高频信号发生器设计—电容三点式振荡电路
  17. matlab spams工具箱,matlab---SPAMS稀疏建模工具箱
  18. 跨境运营培训品牌商店设计技巧
  19. logback官网地址
  20. webpack 的热更新是如何做到的?原理是什么?

热门文章

  1. scapy python_python scapy初探
  2. 1.11编程基础之二分查找 04:网线主管
  3. 10个好用的PPT配图网站,高清无版权,资源多到用不完
  4. 云技术交流群一周只是汇总01
  5. Archlinux安装屏保墙纸软件(livewallpaper)
  6. WordPress开发中常用代码(必备)
  7. V8工作原理(二):垃圾回收:垃圾数据如何自动回收
  8. [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
  9. 11.2.0.4rac service_name参数修改
  10. 东北师范大学计算机学院的导师李俊,东北师范大学信息科学与技术学院导师教师简介-王玉茹...