【问题标题】:C# Save modified image in panelC#将修改后的图像保存在面板中
【发布时间】:2014-04-03 16:16:53
【问题描述】:

我正在尝试保存由图形工具修改的图像,例如可以在图像上绘制的钢笔和形状。我已经使用带有背景图像的面板完成了这项工作,并尝试设置一个位图来保存此面板中的更改:

    private void saveToolStripButton_Click(object sender, EventArgs e)
    {
        //sets panel1 contents as bit map to be saved at set locations
        int width = panel1.Size.Width;
        int height = panel1.Size.Height;

        using (Bitmap bmp = new Bitmap(width, height))
        {
            panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
            bmp.Save(@"C:\Users\Me\Pics\testBitmap.jpeg", ImageFormat.Jpeg);
        }
        MessageBox.Show("Your image has been saved");
     }

单击保存按钮后,图像保存正常,但使用图形工具所做的更改不显示。任何人都可以提出解决方案吗?

这里是一些关于我在面板中使用的图形工具的代码:

    {
        InitializeComponent();
        //Create graphics object in panel1
        g = panel1.CreateGraphics();
    }

    private void btnExit2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    Graphics g;
    //set a drawing boolean
    bool draw = false;
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        draw = true;
        if (drawSq)
        {
            SolidBrush brush = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
            if (toolStripTextBox1.Text != "")
            {
                g.FillRectangle(brush, e.X, e.Y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
            }
            else if (toolStripTextBox1.Text == "")
            {
               MessageBox.Show("Please enter a shape size");
            }

            draw = false;
            drawSq = false; 
        }
    }

还有更多:

      private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        draw = false;
        mouseX = null;
        mouseY = null;
    }
    //null values allow freehand style drawing
    int? mouseX = null;
    int? mouseY = null;
    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        //creates a pen tool and sets properties by mouse location
        if (draw)
        {
            Pen pen = new Pen(btnColor.ForeColor, float.Parse(txtBox1.Text));
            g.DrawLine(pen, new Point(mouseX ?? e.X, mouseY ?? e.Y), new Point(e.X, e.Y));
            mouseX = e.X;
            mouseY = e.Y;
        }
    }

【问题讨论】:

  • “由钢笔和形状等图形工具修改...”我认为我们需要查看该代码或知道它是如何完成的。显然,他们没有在面板上的图像上绘图。
  • 图形工具在面板上运行良好。保存图像时它们根本不包括在内
  • 什么图形工具?
  • 钢笔、画笔、矩形等。它们都可以很好地绘制到面板上
  • 不要使用 CreateGraphics,也不要存储图形对象。从图像中获取图形对象,用它绘制,关闭它。使用面板的绘制事件来绘制更新后的图像。现在您也可以保存该图像了。

标签: c# bitmapimage savefiledialog


【解决方案1】:

让我们尝试解决这个问题。

首先创建您的图像:

Bitmap bmp;

protected override void OnLoad(EventArgs e) {
  base.OnLoad(e); 
  bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);
}

现在当你想在上面画画时:

void panel1_MouseMove(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Left) {
    using (Graphics g = Graphics.FromImage(bmp)) {
      g.FillEllipse(Brushes.Red, new Rectangle(e.X - 4, e.Y - 4, 8, 8));
    }
    panel1.Invalidate();
  }
}

并显示结果:

void panel1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.DrawImage(bmp, Point.Empty);
}

要保存,只需使用您的位图:

bmp.Save(@"c:\filename.png", ImageFormat.Png);

使用Double buffering with Panel 避免闪烁。

【讨论】:

  • 面板图像在其 .BackgroundImage 属性中设置,这与答案有何关联?
  • @ajm 将图像绘制到您的位图中。使用 MouseMove 示例中的代码,但不要使用 g.FillEllipse,而是使用 g.DrawImage
【解决方案2】:

这会很好。我测试了它并且运行良好........

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace drawing
{
    public partial class Form2 : Form
    {
        Graphics g;
        bool startPaint = false;
        int? initX = null;
        int? initY = null;

        bool drawSquare = false;
        bool drawRectangle = false;
        bool drawCircle = false;
        public Form2()
        {
            InitializeComponent();

            bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);

        }
        Bitmap bmp;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

        }
        void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (startPaint)
            {
                using ( g = Graphics.FromImage(bmp))
                {
                  //  g.FillEllipse(Brushes.Black, new Rectangle(e.X, e.Y , 5, 5));

                    Pen p = new Pen(btn_PenColor.BackColor, float.Parse(cmb_PenSize.Text));
                    g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
                    initX = e.X;
                    initY = e.Y;
                    //g.DrawImage(bmp, new Rectangle(e.X - 4, e.Y - 4, 8, 8));
                }
                panel1.Invalidate();
            }
        }
        private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)
        {
            startPaint = true;
             if (drawSquare)
             {
                 //Use Solid Brush for filling the graphic shapes
                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
                 //setting the width and height same for creating square.
                 //Getting the width and Heigt value from Textbox(txt_ShapeSize)
                 g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
                 //setting startPaint and drawSquare value to false for creating one graphic on one click.
                 startPaint = false;
                 drawSquare = false;
             }
             if (drawRectangle)
             {
                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
                 //setting the width twice of the height
                 g.FillRectangle(sb, e.X, e.Y, 2 * int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
                 startPaint = false;
                 drawRectangle = false;
             }
             if (drawCircle)
             {
                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
                 g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
                 startPaint = false;
                 drawCircle = false;
             }
        }
         private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)
        {
            startPaint = false;
            initX = null;
            initY = null;
        }
        void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bmp, Point.Empty);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            bmp.Save("D://filename.jpg", ImageFormat.Png);
        }


    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多