在picturebox中可以拖动图片
来源:互联网 发布:淘宝网电表箱装饰画 编辑:程序博客网 时间:2024/05/29 08:20
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Reflection;
using AForge.Imaging.Filters;
namespace IPLab
{
/// <summary>
/// FilterPreview window
/// </summary>
public class FilterPreview : System.Windows.Forms.Control
{
private Bitmap previewImage;
private Bitmap image;
private IFilter filter;
private Pen blackPen = new Pen( Color.Black, 1 );
private Cursor cursorHand;
private Cursor cursorHandMove;
private int areaWidth, areaHeight;
private int imageX, imageY;
private bool tracking = false;
private int startTrackingX, startTrackingY;
private int oldImageX, oldImageY;
// Image property
[Browsable( false )]
public Bitmap Image
{
get { return image; }
set
{
image = value;
if ( value != null )
{
// calculate size of preview area
areaWidth = Math.Min( ClientRectangle.Width - 2, image.Width );
areaHeight = Math.Min( ClientRectangle.Height - 2, image.Height );
// calculate image position
imageX = ( image.Width - areaWidth ) >> 1;
imageY = ( image.Height - areaHeight ) >> 1;
}
RefreshFilter( );
}
}
// Filter property
[Browsable( false )]
public AForge.Imaging.Filters.IFilter Filter
{
set
{
filter = value;
RefreshFilter( );
}
}
// Constructor
public FilterPreview( )
{
InitializeComponent( );
SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true );
// load cursors
Assembly assembly = this.GetType( ).Assembly;
cursorHand = new Cursor( assembly.GetManifestResourceStream( "IPLab.Resources.harrow.cur" ) );
cursorHandMove = new Cursor( assembly.GetManifestResourceStream( "IPLab.Resources.hmove.cur" ) );
}
// Dispose
protected override void Dispose( bool disposing )
{
if ( disposing )
{
blackPen.Dispose( );
if ( previewImage != null )
{
previewImage.Dispose( );
cursorHand.Dispose( );
cursorHandMove.Dispose( );
}
}
base.Dispose( disposing );
}
// Initialize control
private void InitializeComponent( )
{
this.MouseUp += new System.Windows.Forms.MouseEventHandler( this.FilterPreview_MouseUp );
this.MouseMove += new System.Windows.Forms.MouseEventHandler( this.FilterPreview_MouseMove );
this.MouseDown += new System.Windows.Forms.MouseEventHandler( this.FilterPreview_MouseDown );
}
// Paint control
protected override void OnPaint( PaintEventArgs pe )
{
Graphics g = pe.Graphics;
Rectangle rc = ClientRectangle;
int width = rc.Width;
int height = rc.Height;
int x, y;
// calculate size of preview area
if ( image != null )
{
width = areaWidth + 2;
height = areaHeight + 2;
}
// calculate position of preview area
x = ( rc.Width - width ) >> 1;
y = ( rc.Height - height ) >> 1;
// draw rectangle
g.DrawRectangle( blackPen, x, y, width - 1, height - 1 );
x++;
y++;
if ( image != null )
{
if ( previewImage == null )
{
// draw original image
g.DrawImage( image, new Rectangle( x, y, areaWidth, areaHeight ), imageX, imageY, areaWidth, areaHeight, GraphicsUnit.Pixel );
}
else
{
// draw preview image
g.DrawImage( previewImage, x, y, areaWidth, areaHeight );
}
}
// Calling the base class OnPaint
base.OnPaint( pe );
}
// Refresh preview
public void RefreshFilter( )
{
// release old image
if ( previewImage != null )
{
previewImage.Dispose( );
previewImage = null;
}
if ( ( image != null ) && ( filter != null ) )
{
Bitmap tmp = image.Clone( new Rectangle( imageX, imageY, areaWidth, areaHeight ), image.PixelFormat );
try
{
previewImage = filter.Apply( tmp );
}
catch ( Exception )
{
}
// release temp image
tmp.Dispose( );
}
// repaint
Invalidate( );
}
// On mouse move
private void FilterPreview_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
{
if ( image != null )
{
if ( !tracking )
{
// calculate position of preview area
int x = ( ( ClientRectangle.Width - areaWidth - 2 ) >>1 ) + 1;
int y = ( ( ClientRectangle.Height - areaHeight - 2 ) >>1) + 1;
// check mouse coordinates
if ( ( e.X >= x ) && ( e.Y >= y ) &&
( e.X < x + areaWidth ) && ( e.Y < y + areaHeight ) )
{
Cursor = cursorHand;
}
else
Cursor = Cursors.Default;
}
else
{
int dx = e.X - startTrackingX;
int dy = e.Y - startTrackingY;
imageX = Math.Max( 0, Math.Min( image.Width - areaWidth, oldImageX - dx ) );
imageY = Math.Max( 0, Math.Min( image.Height - areaHeight, oldImageY - dy ) );
Invalidate( );
Cursor = cursorHandMove;
}
}
}
// On mouse button down
private void FilterPreview_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
{
if ( ( image != null ) && ( e.Button == MouseButtons.Left ) )
{
// start tracking
tracking = true;
Capture = true;
startTrackingX = e.X;
startTrackingY = e.Y;
oldImageX = imageX;
oldImageY = imageY;
// release preview image
if ( previewImage != null )
{
previewImage.Dispose( );
previewImage = null;
}
// repaint
// Invalidate( );
// set cursor
Cursor = cursorHandMove;
}
}
// On mouse button up
private void FilterPreview_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
{
if ( tracking )
{
// stop tracking
tracking = false;
Capture = false;
RefreshFilter( );
// set cursor
Cursor = cursorHand;
}
}
}
}
- 在picturebox中可以拖动图片
- 在pictureBox中拖动图片的实现代码
- .net中PictureBox中图片的拖动
- picturebox图片拖动
- C#鼠标拖动图片picturebox
- C#鼠标拖动图片picturebox
- 在winform 中PictureBox放大缩小图片
- PictureBox内的图片拖动功能
- PictureBox内的图片拖动功能
- 在ImageView中拖动图片
- 怎样把网上的图片显示在PictureBox中
- C# 从硬盘托图片在picturebox中打开
- c#+oracle实现读取图片并显示在picturebox中
- PictureBox中拖动鼠标画曲线;
- C#中如何实现拖动PictureBox控件
- C#中,如何随意拖动PictureBox?
- C#中如何实现拖动PictureBox控件
- vb.net中实现picturebox中图片拖动和label控件数组结合,实现label和图片同步
- 模拟title提示效果
- 图的M着色问题
- iPhone开发网站、论坛和博客
- ASP.NET中如何判断UpdatePanel是否完全执行
- 超级网际搜索(SuperSearch) - 4月2日最新版V5.2.41.53,内置搜索引擎更新到147个
- 在picturebox中可以拖动图片
- 关于对学生大本营seo策略及csdn技术搜索引擎的建议(原创)
- FC游戏(NES)模拟器注册破解成功
- quartz任务管理组件
- EXTJS 第二天
- 赶走讨厌的访客的一个小办法
- ASP.Net全局变量的设置和读取方法
- 测试用例设计方法4-正交试验法
- Controlling Area