【问题标题】:Get bitmap and draw it into image获取位图并将其绘制成图像
【发布时间】:2015-06-17 01:55:18
【问题描述】:

我正在尝试将我的绘图从图片框保存到位图中,并将该位图绘制到图像中。到目前为止,最终图像中没有出现任何内容,但是在调试时我只能说,原始位图不为空,并且 with/height 是正确的。但是,我将它绘制成图像后什么都没有出现。

我像这样将我的绘图保存到位图中:

GraphicsPath path = RoundedRectangle.Create(x, y, width, height, corners, RoundedRectangle.RectangleCorners.All);
        g.FillPath(Brushes.LightGray, path);


        g.SetClip(path);

        using (Font f = new Font("Tahoma", 9, FontStyle.Bold))
            g.DrawString(mtb_hotspotData.Text, f, Brushes.Black, textX, textY);
        g.ResetClip();

        bitmap = new Bitmap(width, height, g);

然后保存:

hs.bitmap = new Bitmap(bitmap);

最后使用它:

for (int i = 0; i < imageSequence.Count; i++) {
            Graphics g = Graphics.FromImage(imageSequence[i]);
            //g.CompositingMode = CompositingMode.SourceOver;
            //hotspot.bitmap.MakeTransparent();
            int x = hotspot.coordinates[i].X;
            int y = hotspot.coordinates[i].Y;
            g.DrawImage(hotspot.bitmap, new Point(x, y));
        }


        return imageSequence;

到目前为止,我无法在此解决方案中找到任何问题,因此我不知道故障在哪里。

【问题讨论】:

  • 这些代码位在哪里?
  • 这里有两个类。我在第一类(绘图表格)中绘制并保存图形,并且有一个名为热点的类,其中存储有关此位图(位图,X,Y)的所有信息。
  • 很难找到这种东西。如果是我,我会添加一些代码来将位图保存到代码中合适的位置,然后看看你是否可以找到愚蠢的地方。还可以在各个点最小化和最大化窗口,有时重绘不会在您期望的地方触发。
  • 谢谢,我会尝试将位图保存到文件中。另外我应该注意,在我将位图绘制到图像中后,我会从该图像生成 avi 视频(如果这改变了某些东西)
  • 所以当我保存它时,它是空的。分辨率是正确的,大小 = 165 字节,图标是黑色的,但是当我打开它时,它是透明的。因此,问题(或其中之一)正在从图片框获取它的过程中,对吧?

标签: c# image graphics bitmap picturebox


【解决方案1】:

您似乎误解了BitmapGraphics 对象的关系。

  • Graphics 对象不包含任何图形;它是一种用于将绘制到某种位图的工具。

  • 您使用的Bitmap constructor (public Bitmap(int width, int height, Graphics g)) 确实真正连接 BitmapGraphics 对象。它仅使用来自Graphicsdpi 分辨率。

您没有展示您的Graphics 是如何创建的。如果您想绘制到Bitmap(而不是控件的表面),最直接的方法是:

Bitmap bitmap = new Bitmap(width, height);
bitmap.SetResolution(dpiX, dpiY);  // optional

using (Graphics G = Graphics.FromImage(bitmap ))
{

   // do the drawing..
   // insert all your drawing code here!

}

// now the Bitmap can be saved or cloned..
bitmap.Save(..);
hs.bitmap = new Bitmap(bitmap);  // one way..
hs.bitmap = bitmap.Clone();      // ..or the other

// and finally disposed of (!!)
bitmap.Dispose();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-17
    • 2020-09-02
    • 1970-01-01
    • 2015-06-17
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多