C#,如何使用savefiledialog为类似绘画的程序保存图片

时间:2016-08-09 14:47:41

标签: c#

我尝试了很多方法,包括位图转换等。 这是我的代码。会喜欢有人会向我解释如何保存它以及为什么。谢谢!

glm::mat4 view = glm::mat4(1.0f);



glm::vec3 camRotInRadians = glm::vec3(toRadians(rot.x), toRadians(rot.y), toRadians(rot.z));



glm::vec3 direction = glm::vec3(
    cos(camRotInRadians.x) * cos(camRotInRadians.y),
    sin(camRotInRadians.x),
    cos(camRotInRadians.x) * sin(camRotInRadians.y)
);

// Right vector
glm::vec3 right = glm::vec3(
    cos(toRadians(rot.z)) * cos(toRadians(rot.y + 270.0f)),
    sin(toRadians(rot.z)),
    cos(toRadians(rot.z)) * sin(toRadians(rot.y + 270.0f))
);

glm::vec3 up = glm::cross(right, direction);

view = glm::lookAt(
    pos - direction * glm::vec3(40.0f),          
    pos + direction, 
    up
);

2 个答案:

答案 0 :(得分:0)

试试这个:

panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(dlgSave.FileName);

答案 1 :(得分:0)

编辑问题后我建议您执行以下操作:

  • 在表单中创建一个Bitmap成员,其尺寸与面板相同
  • 从该位图
  • 创建Graphics G
  • 在你的panel1_MouseMove画上Graphics(有效地画入了bitmp`
  • panel1_Paint在面板上绘制位图和
  • button1_Click_2保存该位图:

代码示例:

public partial class Form1 : Form
{
    Bitmap bmp;
    Graphics G;
    Pen myPen = new Pen(Color.Black);
    Point sp = new Point(0, 0);
    Point ep = new Point(0, 0);
    int ctrl = 0;

    public Form1()
    {
        InitializeComponent();

        // create bitmap
        bmp = new Bitmap(panel1.Width, panel1.Height);

        // create Graphics
        G = Graphics.FromImage(bmp);
        G.Clear(Color.Black);

        // redraw panel
        panel1.Invalidate();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        // draw bitmap on panel
        if (bmp != null)
            e.Grahics.DrawImage(bmp, Point.Empty);
    }

    // shortened for clarity

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if(ctrl == 1)
        {
            ep = e.Location;
            // draw onto graphics -> bmp
            G.DrawLine(myPen, sp, ep);
        }
        sp = ep;

        // redraw panel
        panel1.Invalidate();
    }

    private void button1_Click_2(object sender, EventArgs e)
    {
        SaveFileDialog dlgSave = new SaveFileDialog();
        dlgSave.Title = "Save Image";
        dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*";
        if (dlgSave.ShowDialog(this) == DialogResult.OK)
        {
             bmp.Save(dlgSave.FileName);
        }
    }
}