C# 带图形对象的PictureBox缩放模式效果
当您将图像加载到PictureBox中时,会对图像位置进行缩放,如何使用图形对象实现相同的效果?我想你的意思是你想在aC# 带图形对象的PictureBox缩放模式效果,c#,.net,visual-studio-2010,graphics,picturebox,C#,.net,Visual Studio 2010,Graphics,Picturebox,当您将图像加载到PictureBox中时,会对图像位置进行缩放,如何使用图形对象实现相同的效果?我想你的意思是你想在a矩形中绘制一些图像自己,并使用一些图形对象,如PictureBox以缩放模式渲染图像。请尝试以下代码。我假设您要在表单上绘制图像,则应将绘制代码添加到表单的绘制事件处理程序中: //The Rectangle (corresponds to the PictureBox.ClientRectangle) //we use here is the Form.ClientRectan
矩形中绘制一些图像自己,并使用一些图形对象,如PictureBox
以缩放模式渲染图像。请尝试以下代码。我假设您要在表单上绘制图像
,则应将绘制代码添加到表单的绘制
事件处理程序中:
//The Rectangle (corresponds to the PictureBox.ClientRectangle)
//we use here is the Form.ClientRectangle
//Here is the Paint event handler of your Form1
private void Form1_Paint(object sender, EventArgs e){
ZoomDrawImage(e.Graphics, yourImage, ClientRectangle);
}
//use this method to draw the image like as the zooming feature of PictureBox
private void ZoomDrawImage(Graphics g, Image img, Rectangle bounds){
decimal r1 = (decimal) img.Width/img.Height;
decimal r2 = (decimal) bounds.Width/bounds.Height;
int w = bounds.Width;
int h = bounds.Height;
if(r1 > r2){
w = bounds.Width;
h = (int) (w / r1);
} else if(r1 < r2){
h = bounds.Height;
w = (int) (r1 * h);
}
int x = (bounds.Width - w)/2;
int y = (bounds.Height - h)/2;
g.DrawImage(img, new Rectangle(x,y,w,h));
}
我想你的意思是你想在矩形中绘制一些图像
自己,并使用一些图形
对象,就像图片框
在缩放
模式下渲染图像一样。请尝试以下代码。我假设您要在表单上绘制图像
,则应将绘制代码添加到表单的绘制
事件处理程序中:
//The Rectangle (corresponds to the PictureBox.ClientRectangle)
//we use here is the Form.ClientRectangle
//Here is the Paint event handler of your Form1
private void Form1_Paint(object sender, EventArgs e){
ZoomDrawImage(e.Graphics, yourImage, ClientRectangle);
}
//use this method to draw the image like as the zooming feature of PictureBox
private void ZoomDrawImage(Graphics g, Image img, Rectangle bounds){
decimal r1 = (decimal) img.Width/img.Height;
decimal r2 = (decimal) bounds.Width/bounds.Height;
int w = bounds.Width;
int h = bounds.Height;
if(r1 > r2){
w = bounds.Width;
h = (int) (w / r1);
} else if(r1 < r2){
h = bounds.Height;
w = (int) (r1 * h);
}
int x = (bounds.Width - w)/2;
int y = (bounds.Height - h)/2;
g.DrawImage(img, new Rectangle(x,y,w,h));
}
我想你的意思是你想在矩形中绘制一些图像
自己,并使用一些图形
对象,就像图片框
在缩放
模式下渲染图像一样。请尝试以下代码。我假设您要在表单上绘制图像
,则应将绘制代码添加到表单的绘制
事件处理程序中:
//The Rectangle (corresponds to the PictureBox.ClientRectangle)
//we use here is the Form.ClientRectangle
//Here is the Paint event handler of your Form1
private void Form1_Paint(object sender, EventArgs e){
ZoomDrawImage(e.Graphics, yourImage, ClientRectangle);
}
//use this method to draw the image like as the zooming feature of PictureBox
private void ZoomDrawImage(Graphics g, Image img, Rectangle bounds){
decimal r1 = (decimal) img.Width/img.Height;
decimal r2 = (decimal) bounds.Width/bounds.Height;
int w = bounds.Width;
int h = bounds.Height;
if(r1 > r2){
w = bounds.Width;
h = (int) (w / r1);
} else if(r1 < r2){
h = bounds.Height;
w = (int) (r1 * h);
}
int x = (bounds.Width - w)/2;
int y = (bounds.Height - h)/2;
g.DrawImage(img, new Rectangle(x,y,w,h));
}
我想你的意思是你想在矩形中绘制一些图像
自己,并使用一些图形
对象,就像图片框
在缩放
模式下渲染图像一样。请尝试以下代码。我假设您要在表单上绘制图像
,则应将绘制代码添加到表单的绘制
事件处理程序中:
//The Rectangle (corresponds to the PictureBox.ClientRectangle)
//we use here is the Form.ClientRectangle
//Here is the Paint event handler of your Form1
private void Form1_Paint(object sender, EventArgs e){
ZoomDrawImage(e.Graphics, yourImage, ClientRectangle);
}
//use this method to draw the image like as the zooming feature of PictureBox
private void ZoomDrawImage(Graphics g, Image img, Rectangle bounds){
decimal r1 = (decimal) img.Width/img.Height;
decimal r2 = (decimal) bounds.Width/bounds.Height;
int w = bounds.Width;
int h = bounds.Height;
if(r1 > r2){
w = bounds.Width;
h = (int) (w / r1);
} else if(r1 < r2){
h = bounds.Height;
w = (int) (r1 * h);
}
int x = (bounds.Width - w)/2;
int y = (bounds.Height - h)/2;
g.DrawImage(img, new Rectangle(x,y,w,h));
}