din345
asked on
How to convert string to Bitmap
Hi, i want to convert a string (that contains Bitmap-data) to TBitmap, but it doesn't work. here is my code:
var FS: TFileStream;
bmpdat: string;
BMP: TBitmap;
begin
FS := TFileStream.Create('pic.BM P', fmOpenRead);
SetLength(bmpdat, FS.Size);
FS.ReadBuffer(Pointer(bmpd at)^, FS.Size);
FS.Free;
BMP := TBitmap.Create;
BMP.Assign(bmpdat); // something wrong here
Image1.Canvas.Draw(0, 0, BMP);
BMP.Free;
end;
(yes, i know that i can use TBitmap to read the file, but i want to use TFileStream...!)
help me! :)
var FS: TFileStream;
bmpdat: string;
BMP: TBitmap;
begin
FS := TFileStream.Create('pic.BM
SetLength(bmpdat, FS.Size);
FS.ReadBuffer(Pointer(bmpd
FS.Free;
BMP := TBitmap.Create;
BMP.Assign(bmpdat); // something wrong here
Image1.Canvas.Draw(0, 0, BMP);
BMP.Free;
end;
(yes, i know that i can use TBitmap to read the file, but i want to use TFileStream...!)
help me! :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You want to read a image from stream. That can be done much easier. A TBitmap can't process a string that contains bitmapdata. I don't even know if a string is able to contain this type of data. In essence it is a array of byte. If you really want to use a string, things might get a little more complicated.
This is much easier:
var
B: TBitmap;
F: TFileStream;
begin
B := TBitmap.Create;
F := TFileStream.Create ('pic.bmp', fmOpenRead);
try
F.Position := 0;
B.LoadFromStream (F);
// Use bitmap
Image1.Canvas.Draw(0, 0, B);
finally
F.Free;
B.Free;
end;
end;
This is much easier:
var
B: TBitmap;
F: TFileStream;
begin
B := TBitmap.Create;
F := TFileStream.Create ('pic.bmp', fmOpenRead);
try
F.Position := 0;
B.LoadFromStream (F);
// Use bitmap
Image1.Canvas.Draw(0, 0, B);
finally
F.Free;
B.Free;
end;
end;
hello din345 , , you can use a String to store the Bitmap file as data, but the TBitmap, needs a Stream to load an image from, so you might can use a TStringStream to do that, , like this - -
procedure TForm1.sbut_BmpStrStreamCl ick(Sender : TObject);
var FS: TFileStream;
StrStm: TStringStream;
bmpdat: string;
BMP1: TBitmap;
begin
FS := TFileStream.Create('E:\Bmp 1.bmp', fmOpenRead);
SetLength(bmpdat, FS.Size);
FS.ReadBuffer(Pointer(bmpd at)^, FS.Size);
FreeAndNil(FS);
BMP1 := TBitmap.Create;
StrStm := TStringStream.Create(bmpda t) ; // get string as stream
BMP1.LoadFromStream(StrStm );
FreeAndNil(StrStm);
Image1.Canvas.Draw(0, 0, BMP1);
FreeAndNil(BMP1);
end;
procedure TForm1.sbut_BmpStrStreamCl
var FS: TFileStream;
StrStm: TStringStream;
bmpdat: string;
BMP1: TBitmap;
begin
FS := TFileStream.Create('E:\Bmp
SetLength(bmpdat, FS.Size);
FS.ReadBuffer(Pointer(bmpd
FreeAndNil(FS);
BMP1 := TBitmap.Create;
StrStm := TStringStream.Create(bmpda
BMP1.LoadFromStream(StrStm
FreeAndNil(StrStm);
Image1.Canvas.Draw(0, 0, BMP1);
FreeAndNil(BMP1);
end;
why don't you just load that picture, using TBitmap().LoadFromFile(fil ename) ?
var
bmp : TBitmap;
begin
bmp := TBitmap.Create;
bmp.LoadFromFile('pic.BMP' );
var
bmp : TBitmap;
begin
bmp := TBitmap.Create;
bmp.LoadFromFile('pic.BMP'
Bitmap Create or Open
//Copy the string with the Canvas Function into the Bitmap
Canvas.textout(xposition,y
Save, Close the Bitmap
Misunderstanding?