Solved

Bitmap and TStream ... a Q for Madshi

Posted on 2000-02-16
3
330 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
Comment Utility
listenning
0
 
LVL 3

Accepted Solution

by:
philipleighs earned 100 total points
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video discusses moving either the default database or any database to a new volume.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now