2013-11-20 7 views

ответ

3

Я думаю, что вы имеете в виду вы хотите, чтобы сделать некоторые 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; 
} 
Смежные вопросы