2011-08-24 77 views
2

我有picturebox1比我想要加載的圖像大得多。我想要做的就是對準此圖片右側,和圖片框的底部像截圖: enter image description here如何將圖像對齊右,底部在圖片盒

編輯:工作

private void FromCameraPictureBox_Paint(object sender, PaintEventArgs e) 
    { 

     if (loadimage == true) 
     { 
      var image = new Bitmap(@"image.jpg"); 
      if (image != null) 
      { 
       var g = e.Graphics; 
       // -- Optional -- // 
       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       // -- Optional -- // 
       g.DrawImage(image, 
        FromCameraPictureBox.Width - image.Width, // to right 
        FromCameraPictureBox.Height - image.Height, // to bottom 
        image.Width, 
        image.Height); 
      } 

     } 
     loadimage = false; 
    } 

,現在我想從按鈕觸發的paintEvent:

void TestButtonClick(object sender, EventArgs e) 
    { 

     loadimage = true; 
    } 

如何做到這一點?

+0

見我最後的編輯;) – PythEch

回答

4

我很困惑,爲什麼這個代碼工作討厭:

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    var g = e.Graphics; 
    g.DrawImage(pictureBox1.Image, pictureBox1.Width - pictureBox1.Image.Width, pictureBox1.Height - pictureBox1.Image.Height); 
} 

編輯:

好了,現在它的工作原理:

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    var image = Properties.Resources.SomeImage; //Import via Resource Manager 
    //Don't use pictureBox1.Image property because it will 
    //draw the image 2 times. 
    //Make sure the pictureBox1.Image property is null in Deisgn Mode 
    if (image != null) 
    { 
     var g = e.Graphics; 
     // -- Optional -- // 
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
     // -- Optional -- // 
     g.DrawImage(image, 
      pictureBox1.Width - image.Width, // to right 
      pictureBox1.Height - image.Height, // to bottom 
      image.Width, 
      image.Height); 
    } 
} 

UPDATE:

工作:)但是,是否有可能使用這個代碼,而無需 PaintEventsArgs?我試圖添加到我的按鈕標誌和油漆 (IF(標誌==真),然後執行你的代碼,但它不會做任何事情 - 沒有圖紙上picturebox1

這是因爲畫圖事件觸發。一旦我們需要使其重繪默認爲重繪控制方法是Refresh();

在這裏你去:

bool flag = false; 
Bitmap image = null; 
private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    if (flag && image != null) 
    { 
     var g = e.Graphics; 
     // -- Optional -- // 
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
     // -- Optional -- // 
     g.DrawImage(image, 
      pictureBox1.Width - image.Width, 
      pictureBox1.Height - image.Height, 
      image.Width, 
      image.Height); 
    } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    image = new Bitmap("someimage.png"); 
    flag = true; 
    pictureBox1.Refresh(); //Causes it repaint. 
} 

//If you resize the form (and anchor/dock the picturebox) 
//or just resize the picturebox then you will need this: 
private void pictureBox1_Resize(object sender, EventArgs e) 
{ 
    pictureBox1.Refresh(); 
} 
+0

因爲它調整圖像大小。 – PythEch

+0

再次更新。 – PythEch

+0

工作:)但是有沒有可能使用此代碼沒有PaintEventsArgs?我試圖添加到我的按鈕標誌和油漆(如果(標誌==真正),然後執行您的代碼,但它不會做任何事情 - 沒有繪製在picturebox1上 – Elfoc

2

您可以使用位圖+圖形對象和您的圖片的部分複製到一個新的位圖(結果)將被分配給PictureBox的:

Size resultSize = new Size(100, 100); 
Bitmap result = new Bitmap(resultSize.Width, resultSize.Height); 
float left = yourbitmap.Width - resultSize.Width; 
float top = yourbitmap.Height - resultSize.Height; 
using (Graphics g = Graphics.FromImage(result)) 
{ 
    g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    g.DrawImage(yourbitmap, left, top, resultSize.Width, resultSize.Height); 
    g.Save(); 
} 
+0

感謝Strillo,提醒我要設置InterpolationMode :) – PythEch