Advertisement
zoltanleo

bmp/jpg/png from stream

Mar 29th, 2023
1,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.76 KB | None | 0 0
  1. procedure TForm1.btnClearClick(Sender: TObject);
  2. begin
  3.   if Assigned(img1.Picture.Graphic) then
  4.     if not img1.Picture.Graphic.Empty
  5.       then img1.Picture.Graphic:= nil;
  6. end;
  7.  
  8. procedure TForm1.btnLoadClick(Sender: TObject);
  9. var
  10.   ms: TMemoryStream;
  11.   bm: TBitmap;
  12.   png: TPngImage;
  13.   jpg: TJPEGImage;
  14. begin
  15.   bm:= TBitmap.Create;
  16.   png:= TPngImage.Create;
  17.   jpg:= TJPEGImage.Create;
  18.   ms:= TMemoryStream.Create;
  19.   ms.Clear;
  20.   try
  21.     try
  22.       if oDlg.Execute then
  23.       begin
  24.         ms.LoadFromFile(oDlg.FileName);
  25.         ms.Position:= 0;
  26.  
  27.         if (ExtractFileExt(oDlg.FileName) = '.png') then
  28.         begin
  29. //          img1.Picture.LoadFromFile(oDlg.FileName);
  30.           png.LoadFromStream(ms);
  31.           img1.Picture.Assign(png);
  32.         end;
  33.         if ((ExtractFileExt(oDlg.FileName) = '.jpg') or (ExtractFileExt(oDlg.FileName) = '.jpeg')) then
  34.         begin
  35. //          img1.Picture.LoadFromFile(oDlg.FileName);
  36.           jpg.LoadFromStream(ms);
  37.           img1.Picture.Assign(jpg);
  38.         end;
  39.  
  40.         if (ExtractFileExt(oDlg.FileName) = '.bmp') then
  41.         begin
  42. //          img1.Picture.Bitmap.LoadFromFile(oDlg.FileName);
  43.           img1.Picture.Bitmap.LoadFromStream(ms);
  44. //          bm.LoadFromStream(ms);
  45. //          img1.Picture.Bitmap:= bm;
  46.         end;
  47.       end;
  48.     except
  49.       on E: Exception do
  50.       begin
  51.         img1.Picture.Graphic:= nil;
  52.         ShowMessage(e.Message);
  53.       end;
  54.     end;
  55.  
  56.   finally
  57.     ms.Free;
  58.     jpg.Free;
  59.     png.Free;
  60.     bm.Free;
  61.   end;
  62.  
  63. end;
  64.  
  65. procedure TForm1.FormCreate(Sender: TObject);
  66. begin
  67.   oDlg.Filter:= 'Graphic (*.png;*.bmp;*.jpeg;*.jpg;|*.png;*.bmp;*.jpeg;*.jpg;|PNG Files (*.png)|*.png|Bitmap Files (*.bmp)|*.bmp|JPEG Files (*.jpeg;*.jpg)|*.jpeg;*.jpg';
  68. end;
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement