WPF和Winform中picturebox图片局部放大
原文:WPF和Winform中picturebox图片局部放大

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangyisen0713/article/details/19152607

(代码不多,就只放代码了)

一、WPF中图片局部放大

1.xaml中代码:

<Window x:Class="WpfZoom.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WPF局部放大效果" Height="370" Width="700" WindowStartupLocation="CenterScreen"><Canvas x:Name="RootCanvas"><!--左侧小图--><Canvas x:Name="SmallBox" MouseMove="MoveRect_MouseMove" MouseEnter="SmallBox_MouseEnter" MouseLeave="SmallBox_MouseLeave" Width="320" Height="180" Canvas.Left="20" Canvas.Top="20"><Canvas.Background><ImageBrush ImageSource="Images/mm.jpg" Stretch="Fill" TileMode="None" /></Canvas.Background><!--半透明矩形框--><Rectangle x:Name="MoveRect" Fill="White" Opacity="0.3" Stroke="Blue" Width="50" Height="50" Canvas.Top="78" Canvas.Left="202"/></Canvas><!--右侧大图--><Canvas x:Name="BigBox" Width="300" Height="300" Canvas.Left="360" Canvas.Top="20" Visibility="Collapsed"><!--右侧原图片 注意尺寸--><Image x:Name="bigImg" Width="1920" Height="1080" Canvas.Left="0" Canvas.Top="-780" Source="Images/mm.jpg" /><Canvas.Clip><RectangleGeometry Rect="0,0,300,300" /></Canvas.Clip></Canvas></Canvas>
</Window>

2.xaml.cs中代码:

private void MoveRect_MouseMove(object sender, MouseEventArgs e){FrameworkElement element = sender as FrameworkElement;//计算鼠标在X轴的移动距离double deltaV = e.GetPosition(MoveRect).Y - MoveRect.Height / 2;//计算鼠标在Y轴的移动距离double deltaH = e.GetPosition(MoveRect).X - MoveRect.Width / 2; ;//得到图片Top新位置double newTop = deltaV + (double)MoveRect.GetValue(Canvas.TopProperty);//得到图片Left新位置double newLeft = deltaH + (double)MoveRect.GetValue(Canvas.LeftProperty);//边界的判断if (newLeft <= 0){newLeft = 0;}//左侧图片框宽度 - 半透明矩形框宽度if (newLeft >= (this.SmallBox.Width - this.MoveRect.Width)){newLeft = this.SmallBox.Width - this.MoveRect.Width;}if (newTop <= 0){newTop = 0;}//左侧图片框高度度 - 半透明矩形框高度度if (newTop >= this.SmallBox.Height - this.MoveRect.Height){newTop = this.SmallBox.Height - this.MoveRect.Height;}MoveRect.SetValue(Canvas.TopProperty, newTop);MoveRect.SetValue(Canvas.LeftProperty, newLeft);#region//获取右侧大图框与透明矩形框的尺寸比率double n = this.BigBox.Width / this.MoveRect.Width;//获取半透明矩形框在左侧小图中的位置double left = (double)this.MoveRect.GetValue(Canvas.LeftProperty);double top = (double)this.MoveRect.GetValue(Canvas.TopProperty);//计算和设置原图在右侧大图框中的Canvas.Left 和 Canvas.TopbigImg.SetValue(Canvas.LeftProperty, -left * n);bigImg.SetValue(Canvas.TopProperty, -top * n);#endregion}private void SmallBox_MouseEnter(object sender, MouseEventArgs e){BigBox.Visibility = Visibility.Visible;}private void SmallBox_MouseLeave(object sender, MouseEventArgs e){BigBox.Visibility = Visibility.Hidden;}

3.效果:

二、Winform中图片局部放大

1.代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 图片局部放大
{public partial class Form1 : Form{public static int i = 0;private Point m_ptStart = new Point(0, 0);private Point m_ptEnd = new Point(0, 0);private bool m_bMouseDown = false;private float xRate, yRate, realX1, realY1, realX2, realY2;int pLeft = 0;int pTop = 0;public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){xRate = (float)pictureBox1.Image.Width / pictureBox1.Width;yRate = (float)pictureBox1.Image.Height / pictureBox1.Height;SetStyle(ControlStyles.UserPaint, true);SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.SetStyle(ControlStyles.DoubleBuffer, true); //双缓冲}private void pictureBox1_MouseDown(object sender, MouseEventArgs e){if (pictureBox1.HasChildren){for (int i = 0; i < pictureBox1.Controls.Count; i++){pictureBox1.Controls.RemoveAt(0);}}if (e.Button != MouseButtons.Left){return;}m_ptEnd = new Point(e.X, e.Y);this.pictureBox1.Refresh();realX1 = e.X * xRate;realY1 = e.Y * yRate;if (!m_bMouseDown){m_ptStart = new Point(e.X, e.Y);m_ptEnd = new Point(e.X, e.Y);}m_bMouseDown = !m_bMouseDown;}private void pictureBox1_Paint(object sender, PaintEventArgs e){if (m_ptEnd.X - m_ptStart.X < 0 || m_ptEnd.Y - m_ptStart.Y < 0){return;}if (m_ptEnd.X - m_ptStart.X >= 100){m_ptEnd.X = m_ptStart.X + 100;}if (m_ptEnd.Y - m_ptStart.Y >= 100){m_ptEnd.Y = m_ptStart.Y + 100;}e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, m_ptStart.X, m_ptStart.Y, m_ptEnd.X - m_ptStart.X, m_ptEnd.Y - m_ptStart.Y);}private void pictureBox1_MouseUp(object sender, MouseEventArgs e){int eX = 0, eY = 0;if (e.Button != MouseButtons.Left){return;}if (e.X - m_ptStart.X >= 100){if (e.X >= pictureBox1.Width - 1){if (pictureBox1.Width - m_ptStart.X - 1 > 100){eX = m_ptStart.X + 100;}else{eX = pictureBox1.Width - 1;}}else{eX = m_ptStart.X + 100;}}else{if (e.X >= pictureBox1.Width - 1){eX = pictureBox1.Width - 1;}else{eX = e.X;}}if (e.Y - m_ptStart.Y >= 100){if (e.Y >= pictureBox1.Height - 1){if (pictureBox1.Height - m_ptStart.Y - 1 > 100){eX = m_ptStart.Y + 100;}else{eY = pictureBox1.Height - 1;}}else{eY = m_ptStart.Y + 100;}}else{if (e.Y >= pictureBox1.Height - 1){eY = pictureBox1.Height - 1;}else{eY = e.Y;}}if (m_ptStart.X >= 0 && m_ptEnd.X >= 0&& m_ptStart.Y >= 0 && m_ptEnd.Y >= 0&& m_ptStart.X <= 254 && m_ptEnd.X <= 254&& m_ptStart.Y <= 163 && m_ptEnd.Y <= 163){m_ptEnd = new Point(eX, eY);m_bMouseDown = !m_bMouseDown;this.pictureBox1.Refresh();}else{m_ptEnd = new Point(eX, eY);m_ptEnd = m_ptStart;m_bMouseDown = !m_bMouseDown;this.pictureBox1.Refresh();}realX2 = eX * xRate;realY2 = eY * yRate;Crop((Bitmap)pictureBox1.Image);Panel p = new Panel();p.Name = "panel1";p.Location = new Point((int)(realX1 / xRate), (int)(realY1 / yRate));p.Size = new Size((int)(realX2 / xRate - realX1 / xRate), (int)(realY2 / yRate - realY1 / yRate));//p.BackColor = Color.Transparent;p.BackColor = Color.FromArgb(100, 135, 206, 250);//Azure 240 255 255p.BorderStyle = BorderStyle.FixedSingle;p.MouseDown += (s1, e1) =>{pLeft = e1.X;pTop = e1.Y;};p.MouseMove += (s2, e2) =>{GC.Collect();if (e2.Button.ToString().Equals("Left")){if (p.Location.X + e2.X - pLeft <= 1){p.Left = 1;}else if (p.Location.X + e2.X - pLeft >= pictureBox1.Width - p.Width){p.Left = pictureBox1.Width - p.Width - 1;}else{p.Left = p.Location.X + e2.X - pLeft;}if (p.Location.Y + e2.Y - pTop <= 1){p.Top = 1;}else if (p.Location.Y + e2.Y - pTop >= pictureBox1.Height - p.Height){p.Top = pictureBox1.Height - p.Height - 1;}else{p.Top = p.Location.Y + e2.Y - pTop;}}Crop((Bitmap)pictureBox1.Image, Convert.ToInt32(p.Location.X * xRate), Convert.ToInt32(p.Location.Y * yRate), Convert.ToInt32(p.Width), Convert.ToInt32(p.Height));};pictureBox1.Controls.Add(p);}private void pictureBox1_MouseMove(object sender, MouseEventArgs e){if (e.Button != MouseButtons.Left){return;}m_ptEnd = new Point(e.X, e.Y);this.pictureBox1.Refresh();}private void Crop(Bitmap bitmap){if ((int)(realX2 - realX1) > 0 && (int)(realY2 - realY1) > 0){GC.Collect();//GC.WaitForPendingFinalizers();Rectangle rec = new Rectangle((int)realX1, (int)realY1, (int)(realX2 - realX1), (int)(realY2 - realY1));pictureBox2.Image = bitmap.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);}}private void Crop(Bitmap bitmap, int X, int Y, int width, int height){if (width > 0 && height > 0){Rectangle rec = new Rectangle(X, Y, width, height);try{GC.Collect();//GC.WaitForPendingFinalizers();pictureBox2.Image = bitmap.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//bitmap.Dispose();}catch (Exception ex){i++;}finally{}}}}
}

2.效果:

posted on 2018-10-21 23:15 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9827601.html

WPF和Winform中picturebox图片局部放大相关推荐

  1. C#Winform中picturebox控件加载图片后无法释放

    问题描述   最近测试程序功能时发现存在图片资源一直被占用无法释放的问题.   程序的功能大致为打开窗口时从服务器端临时下载图片到本地,然后在窗口中显示,窗口关闭时清除下载的文件.在最后关闭窗口时会报 ...

  2. WPF和winform中使用ZedGraph

    参考使用: 在WPF中使用WinForm控件方法 [ZedGraph]基础 首先添加两个dll文件:WindowsFormsIntegration.dll,System.Windows.Forms.d ...

  3. Winform中实现图片格式转换(附代码下载)

    场景 选择一张照片并选择保存位置和要转换的图片格式实现图片格式转换. 项目运行效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸 ...

  4. Winform中pictureBox控件SizeMode属性

    pictureBox里面的图片大小是不可调整的因为他随着pictureBox控件而变化的 有时候我们会遇到加载图片太长或者太宽了导致图片显示不全 这个时候就使用到了SizeMode里面的Zoom属性了 ...

  5. winform中实现图片左旋,右旋

    左旋 继续旋转 右旋: 继续右旋 效果展示结束: 下面讲讲利用什么来实现的: 由于不光判断是否是图片的*.BMP;*.JPG;*.GIF;*.TIF;*.PNG 还有*.PDF文件,因此写了个自定义控 ...

  6. picturebox显示服务器图片大小,WinForm中实现picturebox自适应图片大小的方法

    本文实例讲述了winform中实现picturebox自适应图片大小的方法.分享给大家供大家参考,具体如下: picturebox控件共有两种载入图片方式,分别为: picturebox1.backg ...

  7. 在winform中使用wpf窗体

    在winform项目,通过引用dll可以添加WPF窗体,如下 但是如果直接在winform的项目中添加wpf窗体还是有部分问题,图片的显示. 直接在XAML界面中用Source属性设置图片会出现错误. ...

  8. c# winform 解决PictureBox 无法打印全部图片的问题

    作者:沐汐 Vicky 出处:http://www.cnblogs.com/EasyInvoice 一. 问题描述 在页面使用PictureBox 加载资料图片后,点击"打印",只 ...

  9. Winform中使用printDocument控件打印pictureBox中的二维码照片

    场景 Winform中使用zxing和Graphics实现自定义绘制二维码布局: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  10. WinForm中使用WPF的控件

    在WinForm中可以使用WPF中的控件,或者由WPF创建的自定义控件: 步骤1:创建WinForm工程: 步骤2:在WinForm工程的解决方案资源管理器中,在刚刚创建的WinForm解决方案中新建 ...

最新文章

  1. 【综述专栏】关于AI Architecture未来的一些思考
  2. json-lib解决死循环
  3. dockerfile的详细介绍
  4. Angular getOrCreateInjectable的实现原理调试
  5. Eclipse 导出
  6. 闲谈简单设计(KISS)疑惑
  7. hibernate 映射_Hibernate映射集合性能问题
  8. ubuntu下安装php redis
  9. Windows 多个系统版本惊现大漏洞,攻击者可随意操作程序!
  10. 雷军藏太深!除小米、迅雷外,还有这么多耳熟能详的企业?
  11. Jquery页面跳转
  12. KMP算法 AC自动机
  13. php gmssl,centos7 php7 gmssl编译安装
  14. 多终端房地产项目管理系统源码,源码分享
  15. VC6.0和VC2012的全局对象的释放!!!
  16. 【路径生成--绘制的方法】矢量地图巡线式路径探索
  17. 第七章:实现、测试。黑盒测试、白盒测试、单元测试、集成测试
  18. 常用概率分布及其数学期望和方差
  19. 使用regedit导入导出环境变量
  20. java 读取xls、xlsx文件

热门文章

  1. nginx相关概念——负载均衡和动静分离
  2. LNMP详解(二)——Nginx源码安装与启动
  3. 一张图看懂OSPF邻接关系建立及报文类型
  4. 使用kubeadm安装部署k8s
  5. 菜单的无限极分类实现
  6. shell-6:shell中的计算$((1+1))
  7. android创建项目,并开发项目。
  8. selenium 自动化web测试
  9. java读加密脚本_尝试将wlst脚本嵌入到java类中时发生加密错误
  10. 5.MongoDB之正则表达式与聚合框架