日期:2014-05-18  浏览次数:21099 次

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)
{