我近日在做一个实习题目要写一个类似于Windows下画笔的东东,快写好了,不过不知道如何将自己画在客户区的图形保存.........各位大虾知道的请回一个,谢了~~

解决方案 »

  1.   

    Graphics g1 = this.CreateGraphics();//获得窗体图形对象 Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1); MyImage.Save(@"c:\test.jpg", ImageFormat.Jpeg);//保存为jpeg文件 
      

  2.   

    GDI+提供保存,托拽需要自己来做,思路:参考MAPINFO,将每个图元包装成类,并添加需要的事件和方法。
      

  3.   

    你可以参考一下codeproject里面的开源drawtool,全部解决了你的问题,它记录绘图的路径,保存时序列化保存到文件,下次打开时还可以编辑,同时也能实现拖拽
    http://www.codeproject.com/csharp/DrawTools/DrawTools_src.zip
      

  4.   

    Graphics g1 = this.CreateGraphics();//获得窗体图形对象 Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1); MyImage.Save(@"c:\test.jpg", ImageFormat.Jpeg);//保存为jpeg文件 
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    我用这种方法保存后出现的图片是一张黑图..........为什么呀??
      

  5.   

    Graphics g1 = this.Create();//获得窗体图形对象 Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1); MyImage.Save(@"c:\test.jpg", ImageFormat.Jpeg);//保存为jpeg文件 
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    这种方法创建的因该是一张和目标Graphics相同分辨率的图片,当然是没有内容的黑图了~~
    要怎么做我也很想知道~~~~
      

  6.   

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
       Graphics mygraphics = this.CreateGraphics();
       Size s = this.Size;
       memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
       Graphics memoryGraphics = Graphics.FromImage(memoryImage);
       IntPtr dc1 = mygraphics.GetHdc();
       IntPtr dc2 = memoryGraphics.GetHdc();
       BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
       memoryImage.Save("c:\\aa.bmp");
       mygraphics.ReleaseHdc(dc1);
       memoryGraphics.ReleaseHdc(dc2);
    }
      

  7.   

    using System;
    using System.Drawing;
    直接运行下面的程序:
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Drawing.Imaging;
    using System.Drawing.Design;
    using System.Drawing.Drawing2D;
    using System.Drawing.Printing;
    using System.Drawing.Text;
    namespace Caculator
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.PictureBox pictureBox1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    using(Bitmap img = new Bitmap(800,800))
      {
    using(Graphics g = Graphics.FromImage(img))
    { Pen WhitePen=new Pen(Color.White,1);
    g.DrawEllipse(WhitePen,150,150,500,500);    

       
     
    }
    img.Save("D:\\e.gif",ImageFormat.Gif);
    } } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components=new System.ComponentModel.Container();
    this.Size=new System.Drawing.Size(300,300);
    this.Text="Display At Starup";
    this.BackColor=Color.White;
    }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    { Application.Run(new Form1());
    }
    }
    }
      

  8.   

    楼上说的方法应该是指在现有的一张图片上进行修改然后保存,可是我现在问题是要像windows下的画笔一样,先在空白的客户区用鼠标绘图然后保存,关键在于我怎么获得客户取得图片???
    至于"wfhlxl(光明正大地偷......学)"的方法,其中的
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    该是自己定义的一个函数吧?我在msdn中没有查到呀?
    再来人呀,我再添分~~~~
      

  9.   

    只要能用就行了麻,把我上面的原封不动的拷到你的程序里,就能在c:\aa.bmp保存了你的窗体上的图案
      

  10.   

    //改一下
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
    Graphics mygraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);
    IntPtr dc1 = mygraphics.GetHdc();
    IntPtr dc2 = memoryGraphics.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
    mygraphics.ReleaseHdc(dc1);
    memoryGraphics.ReleaseHdc(dc2);
    memoryImage.Save("c:\\aa.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); }
      

  11.   

    算是保存了.....连菜单项的图片都保存了,而且图片的下方还有一场条黑色的矩形
    能给解释一下BitBlt这个函数是怎么回事吗??它好像是GDI里面的一个函数,不是GDI+里的
      

  12.   

    最好是那位高手推荐一种用GDI+实现的方法,谢先~~
    我再加20分~~
      

  13.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Text;
    using System.Windows.Forms;namespace testsaveimage
    {
        public partial class Form1 : Form
        {
           
            
            public Form1()
            {
                InitializeComponent();
            }        int i = 5;//画线用的
            System.Drawing.Bitmap image = null;//建立一个实例            
            private void button1_Click(object sender, EventArgs e)
            { 
              
                Graphics g = Graphics.FromImage(pictureBox1.Image);                               g.DrawLine(Pens.Gold,10+i,10,110,10+i);
             
                i += 10;            
                this.pictureBox1.Refresh();            
                g.Dispose();
            }        private void button2_Click(object sender, EventArgs e)
            {
              this.pictureBox1.Image.Save(@"c:\test.jpg", ImageFormat.Gif);        }        private void Form1_Load(object sender, EventArgs e)
            {
                this.pictureBox1.Controls.Clear();
                image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                Graphics.FromImage(image).Clear(Color.White);//消除底图的黑色
                this.pictureBox1.Image = (Bitmap)image.Clone();//这句话是关键
               
            }    }
    }
      

  14.   

    对了,点击button1画线,点击button2保存。上面代码使用vs2005写的
    我这还有一个很好的实例,是从www.codeproject.com下的,需要的话可以给你发过去
      

  15.   

    楼上的代码我知道,我也用过,可现在我的问题是我用一个画板程序,刚运行,自己先在空白的工作区编辑画图,然后要保存我自己画的东西,比如保存为bmp格式,我要怎么把当前的工作区里的内容转化为Image类,然后保存??或者说用别的方法保存也行~~谢了各位大哥
      

  16.   

    pictureBox1.Image就可以得到Image啊!
      

  17.   

    楼上的没错,可是我不用pictureBox那个控件呢?就是在普通的windows窗口中,将客户区的背景属性设置为白色,作为绘图的区域,我要如何获得该区域中我绘制图形的Image类??谢谢~~
      

  18.   

    而且如果我用了pictureBox这个控件,我对其进行修改后能将我修改后的结果保存吗?如何实现??谢谢~