2011-08-24 81 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