Recent

Author Topic: Trouble with converting a JPG to Bitmap  (Read 4238 times)

CynicRus

  • Jr. Member
  • **
  • Posts: 63
Trouble with converting a JPG to Bitmap
« on: July 31, 2012, 10:18:27 am »
Hi. I have some problem with converting jpg to TBitmap.
I'l try that code:
Code: [Select]
function JpegToBitmap(jpeg: string):TBitmap;
var
 jpg:TJpegImage;
 bmp:TBitmap;
 begin
   bmp := TBitmap.Create;
   jpg:=TJpegImage.Create;
   try
   jpg.LoadFromFile(jpeg);
   Bmp.Height      := jpg.Height;
   Bmp.Width       := jpg.Width;
   Bmp.PixelFormat := pf24bit;
   Bmp.Canvas.Draw(0, 0, jpg);
   bmp.Assign(jpg);
   result:=bmp;
   finally
     jpg.Free;
     bmp.Free;
   end;
 end;
but that give me error in Picture.inc in function
Code: [Select]
procedure TPicture.SetGraphic(Value: TGraphic);
var
  NewGraphic: TGraphic;
  ok: boolean;
begin
  if (Value=FGraphic) then exit;
  NewGraphic := nil;
  ok := False;
  try
    if Value <> nil then
    begin
      NewGraphic := TGraphicClass(Value.ClassType).Create;
      NewGraphic.Assign(Value);
      NewGraphic.OnChange := @Changed;
      NewGraphic.OnProgress := @Progress;
    end;
    FGraphic.Free;
    FGraphic := NewGraphic;
    Changed(Self);
    ok := True;
  finally
    // this try..finally construction will in case of an exception
    // not alter the error backtrace output
    if not ok then
      NewGraphic.Free;
  end;
in line  NewGraphic.Assign(Value). How i can convert jpg to TBitmap?

PS: Win7 32, lazarus 0.9.30.4 + fpc 2.6.0.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Trouble with converting a JPG to Bitmap
« Reply #1 on: July 31, 2012, 11:03:59 am »
Code: [Select]
result:=bmp;
   finally
     jpg.Free;
     bmp.Free;
   end;

Your freeing the bitmap, and then returning it.   Remove the bmp.free.

CynicRus

  • Jr. Member
  • **
  • Posts: 63
Re: Trouble with converting a JPG to Bitmap
« Reply #2 on: July 31, 2012, 11:09:15 am »
Big thanks! That is working fine!

 

TinyPortal © 2005-2018