【问题标题】:C# Drawing a Bitmap on a certain point in a PictureBoxC# 在 PictureBox 中的某个点上绘制位图
【发布时间】:2016-10-21 04:19:40
【问题描述】:

我目前有一张Bitmap 图片。我需要将此图像放置在PictureBox 内的某个坐标处。具体来说,我希望将其放置在距顶部 5 个像素和距左侧 5 个像素的位置。位图有不同的长度,所以我希望起点总是在这个特定点开始绘制Bitmap

例如,这里有两个“位图”,它们都从坐标 5,5 开始,具有不同的长度。想象一下灰色是PictureBox


我尝试过传递PictureBox 并使用图形绘制位图:

private void setQuantity(PictureBox pb, int quantity) {

    Graphics g = pb.CreateGraphics();

    g.DrawImage(iqc.createQuantityImage(quantity), 0, 0);

    g.Dispose();

}

iqc.createQuantityImage() returns a Bitmap

但这似乎没有画出任何东西。我也改变了 x 和 y,没有任何变化。

如果可能,我希望能够在PictureBox 内指定确切的坐标或点。

感谢您的帮助!

【问题讨论】:

  • 您的图片框是否也设置了图片?
  • 它有一个BackgroundImage 集,但没有Image

标签: c# graphics bitmap picturebox point


【解决方案1】:

您可以在 PictureBox 中的任意位置绘制图像,方法是在其 Paint 方法中添加事件处理程序,如下所示;

private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(_myBitmap, new Point(5, 5));    //Here new Point(x,y) determines location in pictureBox where you want to draw the bitmap.
}

【讨论】:

  • 谢谢,就是这样。你能解释一下或指向我解释是什么触发了 PictureBox_Paint() 函数的文档吗?我明白了为什么绘图有效,但在调用函数时却没有。
  • Paint 事件在需要重绘该控件时被调用。例如。当窗体或控件调整大小时,将调用 Refresh 或在控件上调用 Invalidate。 This 链接提供了解释。
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2012-08-13
  • 1970-01-01
相关资源
最近更新 更多