首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#绘图放大缩小疑点

2012-04-16 
C#绘图放大缩小问题运用GDI+绘图,比如点画线,点画矩形...如何实现画出来这些图形的放大缩小功能,还要增加

C#绘图放大缩小问题
运用GDI+绘图,比如点画线,点画矩形...
如何实现画出来这些图形的放大缩小功能,还要增加旋转,恢复的功能

[解决办法]
呵呵,希望你多动动脑筋,本想围观,程序员都不容易,还是给你个代码吧(效率不高)
[code=C#]public class ImageViewer : UserControl
{
public Image Image = null;
// 从一个显示一个现有的Image对象
public void ShowImage(Image image)
{
this.Clear(false);

if (image == null)
{
this.Invalidate();
return;
}

this._image = image;
this._size.Width = image.Width;
this._size.Height = image.Height;

// 创建内存位图
this._memImage = new Bitmap((int)this._size.Width, (int)this._size.Height);
this._memGraphic = Graphics.FromImage(this._memImage);
this._memGraphic.DrawImage(this._image, 0, 0, this._size.Width, this._size.Height);

Image = _image;

this.Invalidate();
}

// 图片的原始大小
public void ZoomNormal()
{
this._size.Height = this._image.Height;
this._size.Width = this._image.Width;
this._anchor.X = 0;
this._anchor.Y = 0;
Invalidate();
}

// 放大图片. factor > 0 为放大, factor < 0 为缩小, =0 不变.
public void Zoom(int factor)
{
if (factor == 0)
return;

int zoomx = 0;
int zoomy = 0;

bool mouseInImage = this._mouse.X > this._anchor.X
&& this._mouse.X < this._anchor.X + this._size.Width
&& this._mouse.Y > this._anchor.Y
&& this._mouse.Y < this._anchor.Y + this._size.Height;

if (factor > 0)
{
zoomx = (int)(this._size.Width * this._delta);
zoomy = (int)(this._size.Height * this._delta);

// 如果鼠标在图片是, 以鼠标为中心放大, 此时调整anchor的位置
// 否则, 则按照窗口大小进行位置调整.
if (mouseInImage)
{
float adjx = (float)(this._mouse.X - this._anchor.X) / (float)this._size.Width;
float adjy = (float)(this._mouse.Y - this._anchor.Y) / (float)this._size.Height;
this._anchor.X -= (int)(adjx * zoomx);
this._anchor.Y -= (int)(adjy * zoomy);
}
this._size.Width = this._size.Width + zoomx;
this._size.Height = this._size.Height + zoomy;
}
else
{

if (this._size.Width < this.Width / 2 && this._size.Height < this.Height / 2)
return;

zoomx = (int)(this._size.Width * this._delta);
zoomy = (int)(this._size.Height * this._delta);

if (mouseInImage)
{
float adjx = (float)(this._mouse.X - this._anchor.X) / (float)this._size.Width;
float adjy = (float)(this._mouse.Y - this._anchor.Y) / (float)this._size.Height;

this._anchor.X += (int)(adjx * zoomx);
this._anchor.Y += (int)(adjy * zoomy);
}
this._size.Width = this._size.Width - zoomx;
this._size.Height = this._size.Height - zoomy;
}

Invalidate();
}

// 居中一个边框.
public void CenterRect(Rectangle rect)
{
this.Invalidate();
}

public void Clear(bool bPaint)
{
if (this._image == null)
return;


this._image = null;

if (this._memGraphic != null)
this._memGraphic.Dispose();
if (this._memImage != null)
this._memImage.Dispose();

this._memImage = null;
this._memGraphic = null;

this._anchor.X = 0;
this._anchor.Y = 0;

this._size.Height = 0;
this._size.Width = 0;

this.HotRect = RectangleF.Empty;

this._moving = false;
}

public RectangleF HotRect
{
get
{
return this._hotRect;
}
set
{
if (this._memImage == null)
return;
if (this.HotRect != RectangleF.Empty) // 回复原图像
{
this._memGraphic.DrawImage(this._image, this._hotRect, this._hotRect, GraphicsUnit.Pixel);
}
this._hotRect = value;
this._memGraphic.FillRectangle(this._hotBrsh, this._hotRect);
this.Invalidate();
}
}

public void CenterRect(RectangleF rect)
{
float x = (rect.Left + rect.Width - this.Width) / -2;
float y = (rect.Top + rect.Height - this.Height) / -2;

ScrollDataInfo(this._anchor.X - x, this._anchor.Y - y);
}

public void CenterAndHotRect(RectangleF rect)
{
if (this.HotRect != RectangleF.Empty) // 回复原图像
{
this._memGraphic.DrawImage(this._image, this._hotRect, this._hotRect, GraphicsUnit.Pixel);
}
this._hotRect = rect;
this._memGraphic.FillRectangle(this._hotBrsh, this._hotRect);
//float x = ( rect.Left + rect.Width - this.Width ) / -2;
//float y = ( rect.Top + rect.Height - this.Height ) / -2;
float x = rect.Left + rect.Width / 2 - this.Width / 2;
float y = rect.Top + rect.Height / 2 - this.Height / 2;
ScrollDataInfo(-x, -y);
this.Invalidate();
}

// 构造函数, 执行初始化工作.
public ImageViewer()
: base()
{
this._backBrush = new TextureBrush(Resource.BITMAP_BACKUP); // 背景画刷
this._anchor = new Point(0, 0); // 起始点坐标
}

protected override void OnPaint(PaintEventArgs e)
{
Rectangle clip = e.ClipRectangle;

if (this._memImage == null)
{
e.Graphics.FillRectangle(this._backBrush, clip);
return;
}

e.Graphics.DrawImage(this._memImage, this._anchor.X, this._anchor.Y,
this._size.Width, this._size.Height);

if (this._anchor.X > 0)
e.Graphics.FillRectangle(this._backBrush, 0, 0, this._anchor.X, Height);

if (this._anchor.Y > 0)
e.Graphics.FillRectangle(this._backBrush, 0, 0, Width, this._anchor.Y);

if (this._anchor.X + this._size.Width < this.Width)
e.Graphics.FillRectangle(
this._backBrush, this._anchor.X + this._size.Width,
Math.Max(this._anchor.Y, 0),
this.Width - this._anchor.X - this._size.Width,
this.Height
);

if (this._anchor.Y + this._size.Height < this.Height)
e.Graphics.FillRectangle(
this._backBrush, Math.Max(this._anchor.X, 0),
this._anchor.Y + this._size.Height,
this.Width,


this.Height - this._anchor.Y - this._size.Height
);
}

// 绘制窗口背景.
protected override void OnPaintBackground(PaintEventArgs e)
{
}

// 鼠标滚轮的事件相应.
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta == 0)
return;
Zoom(e.Delta);
}

// 鼠标点击事件
protected override void OnMouseDown(MouseEventArgs e)
{
this.Select();
if (e.Button == MouseButtons.Left && this._image != null)
{
this._moving = true;
this._mouse.X = e.X;
this._mouse.Y = e.Y;
}
else
this._moving = false;
}
code]
[解决办法]

探讨
C# code

// 鼠标抬起事件.
protected override void OnMouseUp(MouseEventArgs e)
{
this._moving = false;
}

protected override void OnMouseMove(Mouse……

[解决办法]
http://www.cnblogs.com/cloudgamer/archive/2010/08/16/ImageTrans.html
 看看这个 功能很全的
[解决办法]
http://www.cnblogs.com/cloudgamer/archive/2010/04/14/ImageZoom_ext.html
[解决办法]
懒人! 非常简单的坐标计算而已,就是不愿动脑筋!


[解决办法]
不太会这放面的 待请教
[解决办法]
简单点,你看msdn中Graphics.DrawImage就可以了。
复杂的就是用坐标转换。多种方式。你如果学GDI+,最不能错过的就是这个GDI+全面的范例了。
http://download.csdn.net/source/2090762

热点排行