TBimap的SaveToFile与SaveToStream有何区别?

sunxking 2002-02-18 09:38:32
加精
我写了以下程序进行跟踪观察,发现对于不同的bmp图片(都是320X240 24位的),
得到的结果不同,有时file.bmp=stream.bmp,而对于另外一些图片,
file.bmp!=stream.bmp,大小不相等.此时file.bmp可用,而stream.bmp就不可
用了,是不是因为savetostream把tbitmap的一些信息也存到了st中?但是据观察,
st->Size的值总是与file.bmp的大小相等,是正确的值,
可是一旦把st写入到文件中后,文件的大小就与file.bmp变得不相等,也就是说st->Size
竟然与文件大小不同!
是不是我的程序有什么错误,可是为何有的bmp就可以呢?
到底SaveToFile与SaveToStream有何区别?
如果我要把bmp1作为bmp格式写入到文件中,不用savetostream,因为我要好几幅
bmp写到同一个文件中,该怎么做?说出方法就给分!!!

TMemoryStream* st=new TMemoryStream;
Graphics::TBitmap* bmp1=new Graphics::TBitmap;
st->Seek(0,soFromBeginning);
bmp1->LoadFromFile("background.bmp");
bmp1->SaveToFile("file.bmp");
bmp1->SaveToStream(st);

char* a=new char[st->Size];
st->Seek(0,soFromBeginning);
st->ReadBuffer(a,st->Size);

FILE* fp;
fp=fopen("stream.bmp","w");
fwrite(a,st->Size,1,fp);
fclose(fp);

delete bmp1;
delete st;
delete a;
...全文
177 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
BCB 2002-02-18
  • 打赏
  • 举报
回复
SaveToFile()总是会清掉原内容,
而流就不同了,与你打开方式有关,如果是读写方式,
文件内容只会增长不会缩短,
所以流文件再写时要清掉内容,
流打开方式应选fmCreate,就一样了.






sunxking 2002-02-18
  • 打赏
  • 举报
回复
就是我没注意w和wb的区别了,
真是感谢啊!高手就是高手,呵呵
我把100全送给你了!
BCB 2002-02-18
  • 打赏
  • 举报
回复
对不起,看了你的原文后,可能是文件打开方式有关
你的fopen()是老式文件系统函数,分二进方式与文本方式
fopen(f,"w"); 缺省是文本方式,会把 0a(换行) 转换成 0d 0a(回车换行)
造成文件长度变长,改成fopen(f,"wb") 应该就可以了;
_fmode==O_TEXT 控制着缺省方式
或都将fopen()改成FileOpen(),后者是API文件系统,不会有这个问题









procedure BitmapFileToPNG(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin Bitmap := TBitmap.Create; PNG := TPNGObject.Create; {In case something goes wrong, free booth Bitmap and PNG} try Bitmap.LoadFromFile(Source); PNG.Assign(Bitmap); //Convert data into png PNG.SaveToFile(Dest); finally Bitmap.Free; PNG.Free; end end; Converting from PNG file to Windows bitmap file The above inverse. Loads a png and saves into a bitmap procedure PNGFileToBitmap(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin PNG := TPNGObject.Create; Bitmap := TBitmap.Create; {In case something goes wrong, free booth PNG and Bitmap} try PNG.LoadFromFile(Source); Bitmap.Assign(PNG); //Convert data into bitmap Bitmap.SaveToFile(Dest); finally PNG.Free; Bitmap.Free; end end; Converting from TImage to PNG file This method converts from TImage to PNG. It has full exception handling and allows converting from file formats other than TBitmap (since they allow assigning to a TBitmap) procedure TImageToPNG(Source: TImage; const Dest: String); var PNG: TPNGObject; BMP: TBitmap; begin PNG := TPNGObject.Create; {In case something goes wrong, free PNG} try //If the TImage contains a TBitmap, just assign from it if Source.Picture.Graphic is TBitmap then PNG.Assign(TBitmap(Source.Picture.Graphic)) //Convert bitmap data into png else begin //Otherwise try to assign first to a TBimap BMP := TBitmap.Create; try BMP.Assign(Source.Picture.Graphic); PNG.Assign(BMP); finally BMP.Free; end; end; //Save to PNG format PNG.SaveToFile(Dest); finally PNG.Free; end end;

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧