0
當您將圖片加載到圖片框中時,圖片放置位置會縮放,如何使用圖形對象實現同樣的效果?使用圖形對象的圖片框縮放模式效果
當您將圖片加載到圖片框中時,圖片放置位置會縮放,如何使用圖形對象實現同樣的效果?使用圖形對象的圖片框縮放模式效果
我想你的意思是你想一些Image
自己繪製在一個Rectangle
和使用一些Graphics
對象等,作爲PictureBox
呈現其形象Zoom
模式。嘗試下面的代碼。我想你想畫的Image
表格,繪圖代碼應該在你的形式的Paint
事件處理程序被添加在:
//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));
}
要測試表單上的完美,你也應該有設置ResizeRedraw = true
和啓用DoubleBuffered
:
public Form1(){
InitializeComponent();
ResizeRedraw = true;
DoubleBuffered = true;
}