小问题:bmp转jpg怎么出错了(但分很高),立解立结 (请老大们看看出错的地方)_(100分)

  • 主题发起人 主题发起人 suke_007
  • 开始时间 开始时间
S

suke_007

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,jpeg;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ScreenCap(LeftPos, TopPos, RightPos, BottomPos: integer);
end;

var
Form1: TForm1;

implementation
var bmpstream:tmemorystream;
{$R *.dfm}

{ TForm1 }


procedure TForm1.ScreenCap(LeftPos, TopPos, RightPos, BottomPos: integer);
var
RectWidth,RectHeight:integer;
SourceDC,DestDC,Bhandle:integer;
bitmap:Tbitmap;
Jpeg1:TJpegImage;
Jpeg2:TJpegImage;//这只是为了测试用的
begin
RectWidth:=RightPos-LeftPos;
RectHeight:=BottomPos-TopPos;
SourceDC:=CreateDC('DISPLAY','','',nil);
DestDC:=CreateCompatibleDC(SourceDC);
Bhandle:=CreateCompatibleBitmap(SourceDC,
RectWidth,RectHeight);
SelectObject(DestDC,Bhandle);
BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,
LeftPos,TopPos,SRCCOPY);
bitmap:=Tbitmap.Create;
Bitmap.Handle:=BHandle;
Jpeg1:= TJpegImage.Create();
Jpeg2:= TJpegImage.Create();
Jpeg1.Assign(Bitmap);
Jpeg1.CompressionQuality:=StrToInt('75');
jpeg1.JPEGNeeded;
Jpeg1.Compress;
jpeg1.savetofile('temp1.jpg');//
Jpeg1.SaveToStream(BmpStream);//在这里就出错,out of system resource????
Jpeg2.loadfromStream(BmpStream);
jpeg2.savetofile('temp2.jpg') ;
Jpeg1.Free;
jpeg2.free;
bitmap.Free;
bmpstream.free;
DeleteDC(DestDC);
ReleaseDC(Bhandle,SourceDC);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
bmpstream:=tmemorystream.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ScreenCap(0, 0, 300,300);
end;

end.
//////
object Form1: TForm1
Left = 192
Top = 107
Width = 322
Height = 172
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 112
Top = 64
Width = 75
Height = 25
Caption = 'capture'
TabOrder = 0
OnClick = Button1Click
end
end

 
我改了一下,注意DeleteDC(DestDC);的位置
procedure TForm1.ScreenCap(LeftPos, TopPos, RightPos, BottomPos: integer);
var
RectWidth, RectHeight: integer;
SourceDC, DestDC, Bhandle: integer;
bitmap: Tbitmap;
Jpeg1: TJpegImage;
Jpeg2: TJpegImage; //这只是为了测试用的
begin
RectWidth := RightPos - LeftPos;
RectHeight := BottomPos - TopPos;
SourceDC := CreateDC('DISPLAY', '', '', nil);
DestDC := CreateCompatibleDC(SourceDC);
Bhandle := CreateCompatibleBitmap(SourceDC,
RectWidth, RectHeight);
SelectObject(DestDC, Bhandle);
BitBlt(DestDC, 0, 0, RectWidth, RectHeight, SourceDC,
LeftPos, TopPos, SRCCOPY);

////DeleteDC(DestDC);放在这里!!!
DeleteDC(DestDC);


bitmap := Tbitmap.Create;
Bitmap.Handle := BHandle;
Jpeg1 := TJpegImage.Create;
Jpeg2 := TJpegImage.Create;
Jpeg1.Assign(Bitmap);
Jpeg1.CompressionQuality := StrToInt('75');
jpeg1.JPEGNeeded;
Jpeg1.Compress;
jpeg1.savetofile('temp1.jpg'); //
Jpeg1.SaveToStream(BmpStream); //在这里就出错,out of system resource????
Jpeg2.loadfromStream(BmpStream);
jpeg2.savetofile('temp2.jpg');
Jpeg1.Free;
jpeg2.free;
bitmap.Free;
bmpstream.free;
ReleaseDC(Bhandle, SourceDC);
end;
 
原来你的是出现out of resource ,DeleteDC(DestDC);要及时调用!释放资源
 
接受答案了.
 
后退
顶部