原文: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.Top
bigImg.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 255
p.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.效果:

WPF和Winform中picturebox图片局部放大的更多相关文章

  1. WPF与Winform中的不同(1)

    1. 部分控件的Text属性,变成了 Content属性 如: winform中,Button.Text = "abc"; wpf中,Button.Content = " ...

  2. C# winform中PictureBox控件的SizeMode模式

    SizeMode属性有五种模式, Normal →标准模式, 在此模式下, 图片位于PictureBox的左上角, 图片的大小由PictureBox控件的大小决定, 当图片的大小大于PictureBo ...

  3. C#winform中调用wpf

    原文:C#winform中调用wpf 在WinForm中是可以使用WPF中的控件(或者由WPF创建的自定义控件) 1.新建一个winform项目: 2.在解决方案上新建一个wpf项目: 如图: 如果有 ...

  4. Winform中Picture控件图片的拖拽显示

    注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...

  5. C#winform中调用wpf(转)

    在WinForm中是可以使用WPF中的控件(或者由WPF创建的自定义控件) 1.新建一个winform项目: 2.在解决方案上新建一个wpf项目: 如图: 如果有如下错误,就在winform中的引用添 ...

  6. C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”

    下面是两段关键代码: /// <summary> /// 将一张图片转换为字节 /// </summary> /// <param name="img" ...

  7. WPF 引用DLL纯图像资源包类库中的图片

    原文:WPF 引用DLL纯图像资源包类库中的图片 1.建立WPF应用程序              过程略.   2.创建类库项目(图片资源包)       创建图片资源类库项目MyImages,删除 ...

  8. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

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

    一.   问题描述 在页面使用PictureBox 加载资料图片后,点击“打印”,只能打印图片首页,较大图片则无法全部打印. 二.   原因分析 PictureBox中打印图片时没有设置继续打印相关属 ...

随机推荐

  1. sum()函数——MATLAB

    a=sum(A)  %列求和 b=sum(A,2) %行求和 c=sum(A(:)) %矩阵求和 假定A为一个矩阵: sum(A)以矩阵A的每一列为对象,对一列内的数字求和. sum(A,2)以矩阵A ...

  2. get_slave_status.py

    #!/usr/bin/env python#-*- encoding: utf8 -*- import mysql.connectorimport get_mysql_conn_info    &qu ...

  3. MVC5管道处理模型

    原文:MVC5管道处理模型 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/82970442 ...

  4. [RxJS] Implement pause and resume feature correctly through RxJS

    Eventually you will feel the need for pausing the observation of an Observable and resuming it later ...

  5. 贝叶斯统计(Bayesian statistics) vs 频率统计(Frequentist statistics):marginal likelihood(边缘似然)

    1. Bayesian statistics 一组独立同分布的数据集 X=(x1,-,xn)(xi∼p(xi|θ)),参数 θ 同时也是被另外分布定义的随机变量 θ∼p(θ|α),此时: p(X|α) ...

  6. ImageButton按压效果失效

    LinearLayout中ImageButton的按压效果不起作用,如图 布局如下: <LinearLayout android:id="@id/ll_add_reply_face&q ...

  7. 使用openoffice转换ms_office to pdf

    java源代码: package com.jeecms.common.office2pdf; import java.io.File; import java.io.FileInputStream; ...

  8. C# 二分法查找和排序

    using System;using System.Collections.Generic;using System.Text; namespace AAA{    public class Dich ...

  9. Java基本数据类型之间赋值与运算归纳

    前言:面对“byte b1=3;byteb2=7;byte b=b1+b2;”报错,而“int i1=3;int i2=7;int i=i1+i2;”不报错,进行了深入探究,从而引申出java基本类型 ...

  10. 小强的HTML5移动开发之路(51)——jquerymobile中改善页面访问速度

    在使用jQuery Mobile进行开发的时候可以选择单页模版和多页模版,在使用单页模版的时候从一个页面跳转到另一个页面的时候需要从服务器请求,用户会感到略有停顿.使用多页模版,可以改善页面跳转之间的 ...