TBitmap,CreateFromStream,delphi

Patrick64 10/21/2016 0

This example demonstrates how to create a TBitmap using the CreateFromStream constructor.

Delphi
 procedure TForm1.Button1Click(Sender: TObject);
var
  // The stream to create the bitmap from
  Stream: TStream;
  // The bitmap to be created
  Bitmap: TBitmap;
begin
  if OpenDialog1.Execute then
  begin
    // Create the stream for the bitmap that will be loaded
    Stream := TFileStream.Create(OpenDialog1.Filename, fmOpenRead or
      fmShareDenyNone);
    try
      // Create the new bitmap
      Bitmap := TBitmap.CreateFromStream(Stream);
      try
        if Bitmap.IsEmpty then
          // Display a message when the bitmap cannot be loaded
          TDialogServiceAsync.MessageDialog(
            Format('Can not load image: %s', [OpenDialog1.Filename]),
            TMsgDlgType.mtWarning, [TMsgDlgBtn.mbOk], TMsgDlgBtn.mbOk, 0)
        else
        begin
          // Copy the new bitmap to the image object, to be displayed
          Image1.Bitmap := Bitmap;
          Label1.Text := Format('Loaded from: %s', [OpenDialog1.Filename]);
        end;
      finally
        Bitmap.Free;
      end;
    finally
      Stream.Free;
    end;
  end;
end; 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments