关于图象的流操作?(请教大虾)(100分)

F

flycity

Unregistered / Unconfirmed
GUEST, unregistred user!
[:D]我用以下的代码截取的屏幕,但是想把它压缩成JPEG但是没效果,但如果我把图片保存在一个文件中再从文件中取出就能成功这是为什么?
dcsource:=createdc('display','','',nil);
dcdest:=createcompatibledc(dcsource);
Bhandle:=createCompatibleBitmap(dcsource,screen.width,screen.height);
selectobject(dcdest,bhandle);
bitblt(dcdest,0,0,screen.Width,screen.Height,dcsource,0,0,srccopy);

map:=tbitmap.Create;
map.Handle:=bhandle;
myjpeg:=TJPEGimage.create;
myjpeg.Assign(map);
myjpeg.CompressionQuality:=10;
myjpeg.Compress;

 
看看以下代码,我试国,没问题的
m1:=tmemorystream.Create; //初始化流m1
bitmap:=tbitmap.Create;
jpg:=tjpegimage.Create;
desk:=tcanvas.Create; //以下代码为取得当前屏幕图象
desk.Handle:=getdc(hwnd_desktop);
with bitmap do
begin
width:=screen.Width;
height:=screen.Height;
canvas.CopyRect(canvas.cliprect,desk,desk.cliprect);
end;
jpg.Assign(bitmap); //将图象转成JPG格式
jpg.CompressionQuality:=30;
jpg.SaveToStream(m1); //file://将JPG图象写入流中
jpg.free;
 
[:)]万分感谢dhycq,我试过了你的代码,没问题,但可否告诉我,我的代码为什么会出错?
而且,(在存入文件的位置)把位图存入流再取出也不行。
 
应该是map没有处理
map:=tbitmap.Create;
map.Handle:=bhandle;
map.savetofile 肯定是空的
加上dhycq的
with map do
begin
width:=screen.Width;
height:=screen.Height;
canvas.CopyRect(canvas.cliprect,desk,desk.cliprect);
end;
应该可以
 
加一条代码就行了
bmp:=tbitmap.Create;
bmp1:=tbitmap.Create;
m1:=tmemorystream.Create;
bmp.LoadFromFile('e:/a.bmp');
bmp.SaveToStream(m1);
m1.Position:=0;//你是不是没有这条
bmp1.LoadFromStream(m1);
canvas.Draw(0,0,bmp1);
 
也就是从流中取数据后要复位指针。
 
首先谢谢大家的热心帮助,我想是我没交待得清楚:
我上段程序的中间有一个空行,行上部分可以截取屏幕(比dhycq的麻烦一些),在空行处加
map.savetofile('c:/wy.bmp');
map.loadfromfile('c:/wy.bmp');能正常得到结果。
但改成: map.savetostream(mystream);
map.loadfromstream(mystream);却报错;
不加语句不出错但没有图形输出。
 
在中间加一句map.position:=0试一下。

map.savetostream(mystream);
map.position:=0
map.loadfromstream(mystream);
 
加上句就可以了。但我还是不明白不加为什么不行?
 
查函数loadfromfile源代码,如果没有那一句函数流程不一样,你查一下,查不到我再
详细解释。
 
不加这句的话在保存完毕后数据指针的位置指向map的末尾,再读的话只能从末尾开始读。
当然会出错。
 
[?][:(][:)]我是说上面的程序段中不加map.savetostream();map.load..
map.savetofile(),map.loadfr..
[?]
 
多人接受答案了。
 
顶部