昨天问过这样的问题“最近作了一个作图软件,让使用者在image上画图,可是在画图时屏幕老是闪,请问为什么?”和“请问如何在paintbox上画图之后实现文件->保存?作图软件中应用什么控件做画布?”没有得到解决。
两者有一个矛盾:用image容易实现文件保存但在画图时屏幕很闪,用paintbox时屏幕不闪但无法实现文件的保存,请问如何解决?谢谢!!!
两者有一个矛盾:用image容易实现文件保存但在画图时屏幕很闪,用paintbox时屏幕不闪但无法实现文件的保存,请问如何解决?谢谢!!!
解决方案 »
- 怎么设置主界面为程序主窗体,登陆界面第一个显示?
- 一个查询语句问题
- 急呀!各位救救我呀。关于添加vcl控件。
- D7怎样处理以前在D5里面做的QReport?
- delphi中adoconnection访问access数据库是不是有bug啊?
- 谁能介绍一下memo的常用方法及属性的 用法?
- 请问的delhi中的包含字符串函数是那个,如何用?
- 我现在是用strtoint和try来检测文本框里的内容是否是数字,不知道还有其他好方法?
- 字符串类型转换成货币类型什么函数?
- 请教能否使Table在打开的时候不预读表的记录到缓冲区中?
- 去看http://expert.csdn.net/Expert/topic/2547/2547583.xml?temp=.8608362,来领分
- 如果整个项目都要用到Variants这个单元,是不是每个单元都要申明?
知道了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;
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了
----------------------------------
需要在onpaint事件中重画一下图形
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.