昨天问过这样的问题“最近作了一个作图软件,让使用者在image上画图,可是在画图时屏幕老是闪,请问为什么?”和“请问如何在paintbox上画图之后实现文件->保存?作图软件中应用什么控件做画布?”没有得到解决。
   两者有一个矛盾:用image容易实现文件保存但在画图时屏幕很闪,用paintbox时屏幕不闪但无法实现文件的保存,请问如何解决?谢谢!!!

解决方案 »

  1.   

    PaintBox怎么不能实现保存了?PaintBox不是有一个Canvas吗,它其实就是封装了DC。
    知道了HDC,还有什么不能保存的呢?比方说,用BitBlt把这个画布拷贝到另一个TBitmap中去。
    var
      bmp: TBitmap;
    begin
      bmp := TBitmap.Create;
      try
        bmp.Width := PaintBox.Width;
        bmp.Height := PaintBox.Height;
        BitBlt(bmp.Canvas.Handle, 0, 0, bmp.Width, bmp.Height,
          PaintBox.Canvas.Handle, 0, 0, SRCCOPY);
        bmp.SaveToFile('C:\myFile.BMP');
      finally
        bmp.Free;
      end;
    end;
      

  2.   

    兩者都是從TGraphicControl繼承過來, 它們沒有本質的區別, 它們的區別是TImage比TPaintBox多了一些東西而已 如Picture,一般都是用TPaintBox.因為Timage中大部份的東西是對你沒什么用的! 當然你也可能用Timage,在做圖形時解決閃的問題有如下方法:
    1 :將Image的parent 如Form (TwinControl)的DoubleBuffer設為True ;
    2 :你做的圖形采用雙Buffer, 在buffer里畫完后用Invalidate通知Timage重畫, 在Onpaint里只需將你Buffer里的內容Copy到Image.Canvas就ok見義你用TPaintBox , 想保存 用如下方法:
    procedure SavetoFile(PaintBox:TPaintBox;FileName:TFileName);
    var
      aBitmap : TBitmap ;
    begin
      Assert(PaintBox<>nil);
      Assert(FileName<>'');
      aBitmap := TBitmap.Create ;
      try
        abitmap.Width := PaintBox.Width ;
        abitmap.Height := PaintBox.Height ;
        abitmap.Canvas.CopyRect(Rect(0,0,PaintBox.Width,PaintBox.Height),
                                PaintBox.Canvas,
                                Rect(0,0,PaintBox.Width,PaintBox.Height));
        abitmap.SaveToFile(FileName);
      finally
        aBitmap.Free ;
      end;
    end;就ok了
      

  3.   

    chinaboylqj ():跟我跟你将的不一样嘛!你自己想想,找些资料来看看吧
      

  4.   

    文件能保存了,但是有一个新问题:切花一下窗口之后画的图像自己消失了,请问怎么回事,如何解决?
    ----------------------------------
    需要在onpaint事件中重画一下图形
      

  5.   

    需要在onpaint事件中重画一下图形
      

  6.   

    短信留言里放不下,帖這吧!!
    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, ExtDlgs;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        PaintBox1: TPaintBox;
        ButtonSave: TButton;
        SavePictureDialog1: TSavePictureDialog;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure PaintBox1Paint(Sender: TObject);
        procedure ButtonSaveClick(Sender: TObject);
      private
      public
        Fbitmap : TBitmap ;
      end;
    var
      Form1: TForm1;
    implementation
    uses Math;
    {$R *.dfm}
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      DoubleBuffered := True ;
      Fbitmap := TBitmap.Create ;
      Fbitmap.Width := PaintBox1.width ;
      Fbitmap.Height := PaintBox1.Height ;
    end;
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    Fbitmap.Free ;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      a : integer ;
      arect : Trect ;
    begin
      a :=50+ Random(100) ;
      Fbitmap.Canvas.Brush.Style := bsClear ;
      fbitmap.Canvas.Pen.Width := 1 ;
      fbitmap.Canvas.Pen.Color := RGB(100+Random(150),100+Random(150),100+Random(150));
      Fbitmap.Canvas.Rectangle(Bounds(Random(fbitmap.Width),Random(fbitmap.Height),a,a));
      arect :=PaintBox1.BoundsRect ;
      windows.InvalidateRect(Self.Handle,@arect,False) ;
    end;
    procedure TForm1.PaintBox1Paint(Sender: TObject);
    begin
      PaintBox1.canvas.CopyRect(Fbitmap.Canvas.ClipRect,Fbitmap.Canvas,Fbitmap.Canvas.ClipRect);end;procedure TForm1.ButtonSaveClick(Sender: TObject);
    begin
      if SavePictureDialog1.Execute then
        Fbitmap.SaveToFile(SavePictureDialog1.FileName);
    end;end.