如何将图像右对齐,底部放在图片框中

我有picturebox1比我想加载的图像大得多。 我想要做的是将此图像对齐到右侧,并在屏幕截图的底部对齐: 在此处输入图像描述

编辑:工作

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; } 

这该怎么做?

我很困惑为什么这段代码令人讨厌:

 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); } } 

更新:

工作:)但是有没有可能在没有PaintEventsArgs的情况下使用这段代码? 我试图添加到我的按钮标志和绘画(if(flag == true)然后执行你的代码,但它没有做任何事情 – 没有绘图picturebox1

那是因为Paint事件会触发一次。 我们需要重绘它。 控件的默认重绘方法是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(); } 

您可以使用位图+图形对象并将图片部分复制到将分配给图片框的新位图(结果):

 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(); }