【问题标题】:Making Bitmap from BufferedGraphics C#从 BufferedGraphics C# 制作位图
【发布时间】:2019-12-26 00:19:57
【问题描述】:

我在 C# WinForms 中制作了一个简单的程序,类似于 MSPaint。我目前卡住的一件事是我想从我正在绘制的 BufferedGraphics 制作 Bitmap 然后保存它。但是当我保存它时,文件中没有任何内容,它只是一个空白图像。

代码如下:

private BufferedGraphics picture;

    public Form1()
    {
        InitializeComponent();
        picture = BufferedGraphicsManager.Current.Allocate(MainPanel.CreateGraphics(), MainPanel.DisplayRectangle);
        picture.Graphics.FillRectangle(Brushes.White, MainPanel.DisplayRectangle);
        picture.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        picture.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    }

    private void Draw()
    {
        if (mouseDown)
        {
            Point cursorPos = MainPanel.PointToClient(Cursor.Position);
            picture.Graphics.FillEllipse(selectedColor, cursorPos.X - slider.Value / 2, cursorPos.Y - slider.Value / 2, slider.Value, slider.Value);
            picture.Render();
        }
    }

    private void Save()
    {
        Bitmap result = new Bitmap(MainPanel.Width, MainPanel.Height, picture.Graphics);

        result.Save("image.png", System.Drawing.Imaging.ImageFormat.Png);

    }

【问题讨论】:

  • 好的文档说什么关于位图构造函数的第三个参数(图形)...我没有看到 将图形复制到位图 ...因为它不能图形对象没有'不包含任何图纸 - 你可以在上面画画,但不能从中获取图纸

标签: c# winforms bitmap


【解决方案1】:

检查下面的代码sn-p;

BufferedGraphics buffer;
int bufferWidth, bufferHeight;

using (Bitmap bitmap = new Bitmap(bufferWidth, bufferHeight))
{
  using (Graphics graphics = Graphics.FromImage(bitmap))
  {
    buffer.Render(graphics);
  }

  bitmap.Save("a.png", ImageFormat.Png);
}

【讨论】:

  • 谢谢,我应该查找BufferderGraphics.Render() 方法的所有参数。我以为只是为了提神。 xD
猜你喜欢
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2020-06-08
  • 2018-04-06
  • 2010-10-15
相关资源
最近更新 更多