TPicture conversion lazarus / 39-Graphics ---------------------------------------------------------------------- xinyiman · 2016-08-17 21:38 Hello guys, a question. I have a TImage named img and people through LoadFromFile procedure. And up to now it all works. The problem is that I then have to make us of the elaboration and comfort I wish if I upload a jpg or png this is loaded in the bitmap. How can I do? So I would like to convert the flight jpg to bmp and png to bmp. Ideas? My actual code to import: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TImgResize.LoadFromFile(filename: string): boolean;var   ret: boolean;begin          try             try                 img.Picture.LoadFromFile(filename);                 ret:=true;            finally            end;          except                on E: Exception do                begin                    ret:=false;               end;          end;          result:=ret;end; ---------------------------------------------------------------------- howardpc · 2016-08-17 21:59 I'm not entirely clear what you want. But perhaps something like this is what you are after? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function LoadImageFromAnyFile(anImage: TImage; const aFilename: string): boolean;var  pic: TPicture;begin  Assert(FileExists(aFilename),'LoadImageFromAnyFile: non-exisistent file');  Assert(anImage<>nil,'LoadImageFromAnyFile: image does not exist');  pic:=TPicture.Create;  try    pic.LoadFromFile(aFilename);  anImage.Picture.Assign(pic);    Result:=not anImage.Picture.Bitmap.Empty;  finally    pic.Free;  end;end; ---------------------------------------------------------------------- derek.john.evans · 2016-08-18 03:27 EDIT: If all you want is the TBitmap, then you will find the TBitmap in TPicture. ie: if you load a JPEG/PNG, you access the TBitmap from TPicture. or, vice-versa. ie: If you load a bmp, you can save out jpeg. All that is already in TPicture. The TPicture object in TImage will aready handle PNG, JPEG & BMP, so there is no need for conversion. Im guessing, since you said "upload", that you want a function which will handle local and remote loading. If so, then something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses StrUtils, fphttpclient; procedure PictureLoadFromUrl(const APicture: TPicture; const AUrl: string);var  LStream: TStream;begin  if AnsiStartsText('http://', AUrl) then begin    LStream := TMemoryStream.Create;    try      TFPHTTPClient.SimpleGet(AUrl, LStream);      LStream.Position := 0;      APicture.LoadFromStream(LStream);    finally      FreeAndNil(LStream);    end;  end else begin    APicture.LoadFromFile(AUrl);  end;end;   Note, this doesn't handle download progress. Todo that, I create a  inherited TMemoryStream class which overrides TStream.Write(). This will allow you to monitor the size of the stream as its downloaded. ---------------------------------------------------------------------- xinyiman · 2016-08-18 09:08 Let's try an example, so we understand each other better. If you try to compile and to run the test source that I have attached. You will see that if I press the button 1 the TImge remains black, if you press the button 2 you will see that an image appears. What I wonder is where I'm wrong if the TImage automatically take care of the automatic conversion as you say. Download the source then you loaded into a jpg file and rename "a.jpg" Then we loaded into a bmp file and rename "a.bmp" Notice that you must have loaded the BGRAControls to fill. ---------------------------------------------------------------------- Michl · 2016-08-18 09:46 For me the result is the same. Button1 and Button2 shows a image (64bit Windows7, 32bit Lazarus trunk, FPC trunk). Maybe you can post your jpg here too, maybe this buggy?! ---------------------------------------------------------------------- xinyiman · 2016-08-18 09:55 My jpeg ---------------------------------------------------------------------- Michl · 2016-08-18 10:22 Your example is also running with Windows7, 32bit and 64bit Lazarus 1.6. Your example failed here too on Linux Mint GKT2 64bit and 32bit Lazarus 1.6. Just a side question. Why don't you use BGRABitmap as a package? Iit wasn't in the dependency. [Edit] If you remove Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     if app.ResizeImage(100,100) then      it run fine on Linux. I'll have a look. ---------------------------------------------------------------------- xinyiman · 2016-08-18 10:35 Quote from: Michl on August 18, 2016, 12:22:38 pmYour example is also running with Windows7, 32bit and 64bit Lazarus 1.6. Your example failed here too on Linux Mint GKT2 64bit and 32bit Lazarus 1.6. Just a side question. Why don't you use BGRABitmap as a package? Iit wasn't in the dependency. [Edit] If you remove Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     if app.ResizeImage(100,100) then      it run fine on Linux. I'll have a look. Ok, but then how do I resize? And then I'm interested to export to file the contents of the bitmap after having resized. Suggestions? ---------------------------------------------------------------------- Michl · 2016-08-18 10:46 One solution, working without BGRABitmap is:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TImgResize.ResizeImage(AWidth: integer; AHeight: integer): boolean;var   ret: boolean;//   Bitmap: TBGRABitmap;         Bitmap: TBitmap;begin          try             try                 //AWidth:img.Picture.Bitmap.Width=AHeight:img.Picture.Bitmap.Height              if AWidth>AHeight then                begin                   //è più largo che alto quindi devo ricalcolare l'altezza in proporzione                     AHeight:=Trunc((AWidth*img.Picture.Bit map.Height)/img.Picture.Bitmap.Width)                end else begin                 //è più lungo che largo quindi devo ricalcolare la larghezza in proporzione                   AWidth:=Trunc((img.Picture. Bitmap.Width*AHeight)/img.Picture.Bitmap.Height)                end;                 Bitmap:=TBitmap.Create;                Bitmap.SetSize(AWidth, AHeight);                Bitmap.Canvas.StretchDraw(Rect(0, 0, AWidth, AHeight), img.Picture.Bitmap);//                Bitmap:=TBGRABitmap.Create(AWidth, AHeight);//                Bitmap.Assign(img.Picture.Bitmap);//                BGRAReplace(Bitmap, Bitmap.Resample(AWidth, AHeight));              img.Picture.Bitmap.Assign(Bitmap);                Bitmap.Free;              ret:=true;              finally                    //codice da effettuare a fine procedura sia che va bene il codice sopra sia che il codice ha sollevato un'eccezzione            end;          except              on E: Exception do                begin                    //codice da eseguire solo se si verifica un eccezzione                 ret:=false;                 end;          end;          result:=ret;end; I'll try to find the reason for BGRABitmap. ---------------------------------------------------------------------- xinyiman · 2016-08-18 10:48 Quote from: Michl on August 18, 2016, 12:46:25 pmOne solution, working without BGRABitmap is:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TImgResize.ResizeImage(AWidth: integer; AHeight: integer): boolean;var   ret: boolean;//   Bitmap: TBGRABitmap;         Bitmap: TBitmap;begin          try             try                 //AWidth:img.Picture.Bitmap.Width=AHeight:img.Picture.Bitmap.Height              if AWidth>AHeight then                begin                   //è più largo che alto quindi devo ricalcolare l'altezza in proporzione                     AHeight:=Trunc((AWidth*img.Picture.Bit map.Height)/img.Picture.Bitmap.Width)                end else begin                 //è più lungo che largo quindi devo ricalcolare la larghezza in proporzione                   AWidth:=Trunc((img.Picture. Bitmap.Width*AHeight)/img.Picture.Bitmap.Height)                end;                 Bitmap:=TBitmap.Create;                Bitmap.SetSize(AWidth, AHeight);                Bitmap.Canvas.StretchDraw(Rect(0, 0, AWidth, AHeight), img.Picture.Bitmap);//                Bitmap:=TBGRABitmap.Create(AWidth, AHeight);//                Bitmap.Assign(img.Picture.Bitmap);//                BGRAReplace(Bitmap, Bitmap.Resample(AWidth, AHeight));              img.Picture.Bitmap.Assign(Bitmap);                Bitmap.Free;              ret:=true;              finally                    //codice da effettuare a fine procedura sia che va bene il codice sopra sia che il codice ha sollevato un'eccezzione            end;          except              on E: Exception do                begin                    //codice da eseguire solo se si verifica un eccezzione                 ret:=false;                 end;          end;          result:=ret;end; I'll try to find the reason for BGRABitmap. Thank you very much ---------------------------------------------------------------------- Michl · 2016-08-18 10:50 The reason is Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Bitmap.Resample(AWidth, AHeight);doesn't work on Linux GTK2. ---------------------------------------------------------------------- Michl · 2016-08-18 11:21 @xinyiman: Just a side note. If a exception is rised, your Bitmap isn't freed. It's better to free it in finally. ---------------------------------------------------------------------- xinyiman · 2016-08-18 12:21 Ok thanks ---------------------------------------------------------------------- Michl · 2016-08-20 19:34 @xinyiman: Your first program should run now, if you update BGRABitmap to the current trunk. See http://forum.lazarus.freepascal.org/index.php/topic,33746 ---------------------------------------------------------------------- xinyiman · 2016-08-24 07:23 Quote from: Michl on August 20, 2016, 09:34:47 pm@xinyiman: Your first program should run now, if you update BGRABitmap to the current trunk. See http://forum.lazarus.freepascal.org/index.php/topic,33746 Thanks