?
Solved

How to convert string to Bitmap

Posted on 2004-09-27
5
Medium Priority
?
1,773 Views
Last Modified: 2013-12-03
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.BMP', fmOpenRead);
 SetLength(bmpdat, FS.Size);
 FS.ReadBuffer(Pointer(bmpdat)^, 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! :)
0
Comment
Question by:din345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 750 total points
ID: 12162301
If you just want to use a filestream, then the following works

var FS: TFileStream;
    bmpdat: string;
    BMP: TBitmap;
begin

  FS:=TFileStream.Create('pic.BMP', fmOpenRead);
  try
     BMP:=TBitmap.Create;
     try
        BMP.LoadFromStream(FS);
        Image1.Canvas.Draw(0, 0, BMP);
     finally
        BMP.Free;
     end;
  finally
     FS.Free;
  end;

end;

---

Regards,
Russell

0
 
LVL 8

Expert Comment

by:BdLm
ID: 12162342
If i understand your question correct, you may use something like this

Bitmap Create or Open
//Copy the string with the Canvas Function into the Bitmap
 Canvas.textout(xposition,yposition, String);
Save, Close the Bitmap

Misunderstanding?
0
 
LVL 1

Expert Comment

by:Bart_Thomas
ID: 12162451
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;
0
 
LVL 34

Expert Comment

by:Slick812
ID: 12163466
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_BmpStrStreamClick(Sender: TObject);
var FS: TFileStream;
    StrStm: TStringStream;
    bmpdat: string;
    BMP1: TBitmap;
begin
 FS := TFileStream.Create('E:\Bmp1.bmp', fmOpenRead);
 SetLength(bmpdat, FS.Size);
 FS.ReadBuffer(Pointer(bmpdat)^, FS.Size);
 FreeAndNil(FS);

 BMP1 := TBitmap.Create;
 StrStm := TStringStream.Create(bmpdat) ; // get string as stream
 BMP1.LoadFromStream(StrStm);
 FreeAndNil(StrStm);
 Image1.Canvas.Draw(0, 0, BMP1);
 FreeAndNil(BMP1);
end;
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 12173672
why don't you just load that picture, using TBitmap().LoadFromFile(filename) ?

var
  bmp : TBitmap;
begin
bmp := TBitmap.Create;
bmp.LoadFromFile('pic.BMP');
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question