我正在使用 DrawImage 调整图像大小。
Graphics.DrawImage(SourceImage,0,0,200,200);
这里源图像从坐标 0 开始。
假设我需要动态计算 x 和 y 坐标,我该怎么做?
默认情况下,图像应从位置 20(即 x)和 20(即 y)开始。
如果我调整表格的大小,它应该根据调整后的图像按比例计算,这意味着,如果默认是 20,那么表格调整大小是多少?
谢谢
您可以注册到窗体的 ResizeEnd 事件并可以重绘图像。就像是;
public Form1()
{
InitializeComponent();
this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
}
void Form1_ResizeEnd(object sender, EventArgs e)
{
//draw the image again using the related calculation
}
从您的问题来看,尚不清楚表单的大小与所需坐标的关系。
表单具有ClientRectangle
可用于计算坐标的属性。例如,如果您想在右下角显示图像,您可以:
protected override void OnPaint(PaintEventArgs e)
{
int x = this.ClientRectangle.Width - 200;
int y = this.ClientRectangle.Height - 200;
e.Graphics.DrawImage(SourceImage, x, y, 200, 200);
}
我假设DrawImage
代码在Paint
事件处理程序中,然后您可以
SetStyle(ControlStyles.ResizeRedraw, true);
在表单构造函数中使用,因此Paint
在调整表单大小时调用Resize
并调用Invalidate();
自己