Solved

Bitmap and TStream ... a Q for Madshi

Posted on 2000-02-16
3
333 Views
Last Modified: 2013-12-03
Madshi,
referencing another question that you provided and answer for
 
Date: Sunday, February 06 2000 - 10:32PM PST
Delphi : Q.10291114

From: baudewyns
Title: "Icon and TStream

 How can I , or if it is possible to adapt the
Icon and TStream program to a Bitmap and TStream? Obviously, at least to me, there was no adaptation that is a substitute for  
 the program line    >>>> DrawIcon(Canvas.Handle, 0, 0, mbp.handle);
since DrawIcon must be a primitive and  DrawBitmap is not.

Delphi3




0
Comment
Question by:delphi3
3 Comments
 
LVL 3

Expert Comment

by:bryan7
ID: 2528687
listenning
0
 
LVL 3

Accepted Solution

by:
philipleighs earned 100 total points
ID: 2529747
Do you mean a Windows Bitmap (HBITMAP) or a Delphi bitmap (TBitmap)?

If TBitmap, then how about:

Canvas.Draw(0, 0, MyBitmap);
0
 
LVL 4

Author Comment

by:delphi3
ID: 2530109
philipleighs.
Congratulations. A super bit of work. Here is the completed work, with your sparkle line in place.

Thanks a lot,

Delphi3



unit BmpToAndFromStreamUnit1;
interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
// use mbp for ico and TBitmap for TIcon  and Btest.dat for test.dat

procedure SaveToStream(str1: string; mbp: TBitmap; str2: string);
var
  i1: integer;
  ms: TMemoryStream;
begin
  with TFileStream.Create('C:\WINDOWS\Desktop\To.From Stream\Btest.dat', fmCreate) do
  try
    i1 := Length(str1);
    Write(i1, 4);
    Write(pointer(str1)^, i1);
    ms := TMemoryStream.Create;
    try
      mbp.SaveToStream(ms);
      i1 := ms.Size;
      Write(i1, 4);
      Write(ms.Memory^, ms.Size);
    finally ms.Free end;
    i1 := Length(str2);
    Write(i1, 4);
    Write(pointer(str2)^, i1);
  finally Free end;
end;

function LoadFromStream(var str1, str2: string): TBitmap;
var
  i1: integer;
  ms: TMemoryStream;
begin
  with TFileStream.Create('C:\WINDOWS\Desktop\To.From Stream\Btest.dat', fmOpenRead) do
  try
    Read(i1, 4);
    SetLength(str1, i1);
    Read(pointer(str1)^, i1);
    ms := TMemoryStream.Create;
    try
      Read(i1, 4);
      ms.Size := i1;
      Read(ms.Memory^, i1);
      result := TBitmap.Create;
      result.LoadFromStream(ms);
    finally ms.Free end;
    Read(i1, 4);
    SetLength(str2, i1);
    Read(pointer(str2)^, i1);
  finally Free end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s1, s2: string;
  mbp: TBitmap;
begin
  mbp := TBitmap.Create;
  mbp.LoadFromFile('C:\WINDOWS\Desktop\To.From Stream\testB.bmp'); // give in a valid bmp file here
  SaveToStream('Btest1', mbp, 'Btest2');
  mbp.Free;
  mbp := LoadFromStream(s1, s2);
  Canvas.Draw(0,0, mbp);
  mbp.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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