如何Graphics对象保存在C#中的形象? [英] How to save Graphics object as image in C#?

查看:137
本文介绍了如何Graphics对象保存在C#中的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有面板和各种控件。我想这个面板的图像保存到一个文件,我怎么能做到这一点?

I have panel and various controls on it. I would like to save an image of this panel into a file, how can I do this ?

Ineed像做截图,但我只需要在我的应用程序的某些面板的形象,我想这样做,在我的应用程序按钮点击。

Ineed to do something like screenshot, but I need just image of certain panel in my application and I want to do this on a button click in my app.

最好的问候,普里莫兹

编辑:
我也画使用此代码

I also draw on this panel using this code

            Graphics g = chartTemperature.CreateGraphics();    
            g.DrawLine(p, prevPoint, e.Location);
            prevPoint = e.Location;



但我不进入这个图片的。为什么,以及如何解决这一问题?

But then I don't get this into image. Why, and how to fix this ?

EDIT 2:

编辑2 $ C>命名空间Grafi
{
公共部分Form1类:表格
{

布尔isDrawing = FALSE;
点prevPoint;

公共Form1中()
{
的InitializeComponent();
}

私人无效chartTemperature_MouseDown(对象发件人,MouseEventArgs E)
{
isDrawing = TRUE;
prevPoint = e.Location;
}

私人无效chartTemperature_MouseMove(对象发件人,MouseEventArgs E)
{
笔P =新朋(Color.Red,2);
如果(isDrawing)
{
图形G = chartTemperature.CreateGraphics();
g.DrawLine(对,prevPoint,e.Location);
prevPoint = e.Location;

numOfMouseEvents = 0;
}
p.Dispose();
}

私人无效chartTemperature_MouseUp(对象发件人,MouseEventArgs E)
{
isDrawing = FALSE;
}
}
}

namespace Grafi { public partial class Form1 : Form { bool isDrawing = false; Point prevPoint; public Form1() { InitializeComponent(); } private void chartTemperature_MouseDown(object sender, MouseEventArgs e) { isDrawing = true; prevPoint = e.Location; } private void chartTemperature_MouseMove(object sender, MouseEventArgs e) { Pen p = new Pen(Color.Red, 2); if (isDrawing) { Graphics g = chartTemperature.CreateGraphics(); g.DrawLine(p, prevPoint, e.Location); prevPoint = e.Location; numOfMouseEvents = 0; } p.Dispose(); } private void chartTemperature_MouseUp(object sender, MouseEventArgs e) { isDrawing = false; } } }

这是我的画的代码画一个自定义的上线图表。你可以帮我做正确的方法是什么?

This is my drawing code to draw a custom line onto a Chart. Can you please help me to do it proper way ?

推荐答案

使用的Control.DrawToBitmap()方法。例如:

Use the Control.DrawToBitmap() method. For example:

    private void button1_Click(object sender, EventArgs e) {
        using (var bmp = new Bitmap(panel1.Width, panel1.Height)) {
            panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            bmp.Save(@"c:\temp\test.png");
        }
    }

这篇关于如何Graphics对象保存在C#中的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆