Solved

Bitmap and TStream ... a Q for Madshi

Posted on 2000-02-16
3
331 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now