Solved

Getting a bitmap from Timage into which I've loaded jpg, png, or gif file

Posted on 2011-03-08
19
2,647 Views
Last Modified: 2012-05-11
I have the following code in a Delphi procedure.  It works fine when I load a .bmp file but when I load a jpg, png or gif, although I can properly display the loaded image (by putting the picture into a visible Timage, I can't access the bitmap that I need to pass to my AddBitMap procedure.

Obviously I'm doing something wrong but I've tried everything I can think of to no avail!

Does anyone have the answer that has eluded me?
<snip>
    img:=TImage.Create(frmOD);
    img.AutoSize:=True;
    img.Picture.LoadFromFile(dlgOpenPic1.FileName); // .jpg, .png, .gif, .bmp ??
    bm:=TBitmap.Create;
    bm.Height:=img.Height;
    bm.width:=img.Width;
    bm.assign(img.Picture.bitmap);
    _form.AddBitMap(bm);
    bm.Free;
    img.Free;
</snip>
0
Comment
Question by:DMTrump
  • 9
  • 7
  • 3
19 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079182
use this JPeg

uses
  JPeg;
var
  B: TBitmap;
  J: TJPegImage;
  img:TImage;
begin
  
  J := TJPegImage.Create;
  J.LoadFromFile(dlgOpenPic1.FileName);
  img:=TImage.Create(frmOD);
  img.picture.Graphic.Assign(J);
  j.Free;
  img.AutoSize:=True;

    bm:=TBitmap.Create;
    bm.Height:=img.Height;
    bm.width:=img.Width;
    bm.assign(img.Picture.bitmap);
    _form.AddBitMap(bm);
    bm.Free;
    img.Free;

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079311
This is better and cleaner, add code to check the extension if it is JPeg otherwise load the normal way
var
  bm: TBitmap;
  J: TJPegImage;
  img: TImage;
  FileExt: string;
begin
  img:=TImage.Create(frmOD);
  try
    FileExt := ExtractFileExt(dlgOpenPic1.FileName);
    if Pos('jpg', LowerCase(FileExt)) > 0 then
    begin
      J := TJPegImage.Create;
      try
        J.LoadFromFile(dlgOpenPic1.FileName);
        img.picture.Graphic.Assign(J);
      finally
        FreeAndNil(J);
      end;
    end
    else
      img.Picture.LoadFromFile(dlgOpenPic1.FileName)

    img.AutoSize:=True;
    bm:=TBitmap.Create;
    try
      bm.Height:=img.Height;
      bm.width:=img.Width;
      bm.assign(img.Picture.bitmap);
      _form.AddBitMap(bm);
    finally
      FreeAndNil(bm);
    end;
  finally
    FreeAndNil(img);
  end;
end;

Open in new window

0
 

Author Comment

by:DMTrump
ID: 35079313
Yes I had found that offered as a solution elsewhere - but it's not completely what I want.  That would handle it if I determine that I'm loading a jpeg file - but what about when someone loads a Gif or Png?  
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079328

Look at my second solution, it checks for Jpg
0
 
LVL 2

Accepted Solution

by:
RezaSadigh earned 350 total points
ID: 35079368
Hi my friend,
img.Picture.bitmap contain data only if image has a bmp picture. so your program works fine if your input picture is bmp otherwise img.Picture.bitmap is empty.
you can use img.Picture.graphic instead of img.Picture.bitmap so in your code
bm.assign(img.Picture.bitmap); ---> bm.assign(img.Picture.Graphic);
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 150 total points
ID: 35079407
Actually you don't even have to use TJPegImage component

In your interface section, just add the unit JPeg and the TImage component will be able to load the jpg image properly

interface
 
uses
  Windows, Classes, Forms,.........., JPeg;

By inluding the JPeg unit to your form, the jpg format is registered in your application
The initialization section of the JPeg unit has this code which does the registration

initialization
  InitDefaults;
  TPicture.RegisterFileFormat('jpeg', sJPEGImageFile, TJPEGImage);
  TPicture.RegisterFileFormat('jpg', sJPEGImageFile, TJPEGImage);

Once this is done, you can just use your original code

 img.Picture.LoadFromFile(dlgOpenPic1.FileName); // .jpg, .png, .gif, .bmp ??



0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079478

@RezaSadigh
Good point
0
 

Author Comment

by:DMTrump
ID: 35079490
I've tried your second example and I get a fault on the
img.picture.Graphic.Assign(J);

Any further hints?
0
 

Author Comment

by:DMTrump
ID: 35079528
Also, this would only satisfy for bmp and jpg - what can I do for png and gif?
0
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.

 

Author Comment

by:DMTrump
ID: 35079552
Trying RezaSadigh hint right now
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079569

You need to use the bitmap property
img.picture.Bitmap.Assign(J);

0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35079595
Ok,
something should become clear,
what is your delphi version? TImage.Picture.LoadFromFile on native form can't load PNG.
Are you using some extra code for doing that.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079596

For GIF and PNG, you will need third party components since these are not provided by Delphi

for GIF take a look at TGIFImage component
http://www.tolderlund.eu/delphi/


I have not worked with PNG images in delphi but you can find a component for that as well
0
 

Author Closing Comment

by:DMTrump
ID: 35079631
Actially I was already using the JPEG unit so all I needed to do was use the  bm.assign(img.Picture.Graphic); suggested by   RezaSadigh  - however ewangoya's comments were helpful too and I like to share the points when several people have contributed!

Thank you my friends  I now have code that works for all file types!
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079639

As I mentioned earlier in #35079407, if you include JPeg, GIFImage in your uses clause in the interface section, you don't need any special method to load the images, just call

img.Picture.LoadFromFile(dlgOpenPic1.FileName);
0
 

Author Comment

by:DMTrump
ID: 35079648
Yes, to your hints about components for handling png and gif - I'd done that so long ago on this project that I'd forgotten about it (also of course I was already using the jpeg unit.  Everything was working fine until I needed to pur the graphic int a bitmap for another piece of functionality.  Thanks again!
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35079649

Glad to help
0
 

Author Comment

by:DMTrump
ID: 35079687
So just a final clarification:

I already had JPEG, GIFImg and PNGimage in my uses clause The only part that was missing was using the Graphic.Assign(bm) instead of BitMap.assign(bm)  

And by the way - I'm currently on Delphi 2007
0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35079689
Best regards
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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

10 Experts available now in Live!

Get 1:1 Help Now