GDI+的问题:怎么才能创建并保存图形文件?
var
ImgD: TGPImage;
EClsid: TGUID;
begin
ImgD:=TGPImage.Create;
grp:=TGPGraphics.Create(ImgD);
brush:=TGPSolidBrush.Create(FillColor);
grp.FillRectangle(brush,0,0,NW,NH);
GetEncoderClsid('image/jpeg',EClsid);
ShowMessage(GetStatus(ImgD.save('C:\1.jpg',EClsid)));结果Save失败:
---------------------------
Project1
---------------------------
InvalidParameter
---------------------------
OK
---------------------------
var
ImgD: TGPImage;
EClsid: TGUID;
begin
ImgD:=TGPImage.Create;
grp:=TGPGraphics.Create(ImgD);
brush:=TGPSolidBrush.Create(FillColor);
grp.FillRectangle(brush,0,0,NW,NH);
GetEncoderClsid('image/jpeg',EClsid);
ShowMessage(GetStatus(ImgD.save('C:\1.jpg',EClsid)));结果Save失败:
---------------------------
Project1
---------------------------
InvalidParameter
---------------------------
OK
---------------------------
encoderParams: PEncoderParameters = nil): TStatus;
GetEncoderClsid('image/jpeg',EClsid,nil);
ImgD: TGPImage;
EClsid: TGUID;
Grp : TGPGraphics;
brush: TGPSolidBrush;
pen: TGPPen;
begin
ImgD:=TGPImage.Create();
grp:=TGPGraphics.Create(ImgD);
brush:=TGPSolidBrush.Create(makeColor(255,0,0));
grp.FillRectangle(brush,0,0,200,200);
pen:=TGPPen.Create(makeColor(0,0,0),5);
grp.DrawLine(pen,1,1,100,100);
GetEncoderClsid('image/jpeg',EClsid);
ShowMessage(GetStatus(ImgD.save('C:\4.jpg',EClsid,nil)));也是一样不成功的。
GpImage : TGPImage;
Bmp : TBitmap;
Grp : TGPGraphics;
begin
GpImage := TGPImage.Create('c:\1.png');
Bmp := TBitmap.Create;
Bmp.Width := GpImage.GetWidth;
Bmp.Heiht := GpImage.GetHeight;
gr := TGPGraphics.Create(Bmp.Canvas.Handle); gr.Drawimage(GpImage,makerect(0,0,Bmp.Width,Bmp.Height),0,0,GpImage.GetWidth,Gpimage.GetHeight,UnitPixel);
Gr.Free;
GpImage.Free;
Bmp.savetofile('c:\1.bmp');
Bmp.Free;
end;
以前用的是从盒子上下载的几个PAS单元,翻译了GDI+的头文件和代码的。
感觉用起来没这么麻烦的
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, GDIPAPI, GDIPOBJ, StdCtrls;type
TForm1 = class(TForm)
Read: TButton;
Memo1: TMemo;
procedure ReadClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ReadClick(Sender: TObject);
var
Image: TGPImage;
size: UINT;
propertyItem: PPropertyItem;
x: array[0..31] of byte;
i:integer;
Str: String;
type
ac= array of byte;
begin
Image:= TGPImage.Create('..\untitled.JPG'); // Assume that the image has a property item of type PropertyItemEquipMake.
// Get the size of that property item.
size := image.GetPropertyItemSize(PropertyTagImageTitle); // Allocate a buffer to receive the property item.
//propertyItem = (PropertyItem*)malloc(size);
GetMem(propertyItem ,Size); // Get the property item.
image.GetPropertyItem(PropertyTagImageTitle, size, propertyItem); // Display the members of the retrieved PropertyItem object.
memo1.Lines.Add(format('The length of the property item is %u.', [propertyItem.length]));
memo1.Lines.Add(format('The data type of the property item is %u.', [propertyItem.type_]));
for i := 0 to 31 do x[i] := ac(propertyItem)[i];
if(propertyItem.type_ = PropertyTagTypeASCII) then
begin
Str := PChar(propertyItem.value);
memo1.Lines.Add(format('The value of the property item is %s.', [str]));
end;
freemem(propertyItem);
image.Free;
end;end.
Button: TButton;
Image: TImage;
procedure ButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
SaveDrawForm: TSaveDrawForm;implementationuses
GDIPAPI, GDIPOBJ, GDIPUTIL;{$R *.dfm}procedure TSaveDrawForm.ButtonClick(Sender: TObject);
var
Graphics: TGPGraphics;
Bmp: TGpBitMap;
SolidBrush: TGPSolidBrush;
EncoderClsid: TGUID;
begin
//画在Bmp画布里面
Bmp := TGpBitMap.Create(100,100, PixelFormat32bppRGB);
Graphics := TGPGraphics.Create(Bmp);
SolidBrush:= TGPSolidBrush.Create(MakeColor(255, 255, 0, 0));
Graphics.FillRectangle(SolidBrush, 0, 0, 100, 100); //显示在Image画布上
{Image.Width := Bmp.GetWidth;
Image.Height := Bmp.GetHeight;
Image.Left := 0;
Image.Top := 0; }
Graphics := TGPGraphics.Create(Image.Canvas.Handle);
Graphics.DrawImage(Bmp, 0, 0, Image.Width, Image.Height); //保存为外部图片
GetEncoderClsid('image/jpeg', EncoderClsid);
Bmp.Save('C:\1.jpg', EncoderClsid, nil); //释放对象
SolidBrush.Free;
Graphics.Free;
Bmp.Free;
end;
ImgD:=TGPImage.Create;
改为
ImgD:=TGPImage.Create(宽度, 高度);就ok
===================================================================================
谁是谬论试一下不就行了吗?
===================================================================================
不就是把TGpImage改为TGpBitmap吗?回错了直接指出不行吗?何必用那种口气!我用的GDI+就是自己写的,这有什么难的?
var
ImgD: TGPImage;
EClsid: TGUID;
g: TGpGraphics;
brush: TGpBrush;
w, h: Integer;
begin
w := 100; h := 100;
ImgD:=TGPBitmap.Create(w, h);
g:=TGPGraphics.Create(ImgD);
brush:=TGPSolidBrush.Create(aclRed);
g.FillRectangle(brush,0,0,w,H);
GetEncoderClsid('image/jpeg',EClsid);
ImgD.save('C:\1.jpg',EClsid);
end;
我是无业人员,现在不困,不知你也是否无事,还在这里聊
我看过你回的很多帖,觉得你思路敏捷,每个方面都好像精通似的,说实在话,我市很佩服的,我不过是个业余爱好者,而且年纪大,记忆差,同你们是比不了的
http://community.csdn.net/Expert/topic/5182/5182341.xml?temp=.7231409