Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

rgb colour values of a jpeg image?

Posted on 2000-05-11
5
560 Views
Last Modified: 2013-11-19
Hi guys!

I have a form with a Timage (in Delphi 5) and I loaded it with a jpeg file. I want to know the RGB values for a particular pixel in the jpeg file.

If I load the Timage with a bmp file, I can find the rgb values easily using this code:

      L := Form1.Image1.Picture.Bitmap.Canvas.Pixels[x,y];
      R := R + ((L shr 16) and $FF);
      G := G + ((L shr 8) and $FF);
      B := B + (L and $FF);

It does not work with jpeg images. Any suggestion?

thanks in advance,
Manuel López (lopem)
0
Comment
Question by:lopem
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:raidos
ID: 2803577
I beleive that the JPG is not in Image1.Picture.Bitmap but rather
Image1.Picture.Graphic

But i don't know how to address the canvas for jpg's

Maybe you need to convert it to bitmap and draw it into the Image1.Picture.Bitmap.Canvas yourself

Good luck

//raidos
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2803607
TJPEGImage is a TGraphic descentant. This causes TPicture to throw away the image data when accessing its Bitmap property. The solution is to use a separate TPicture to load the JPG and do an Bitmap.Assign(JPGPicture.Graphic) to the image before acessing its canvas.

Ciao, Mike
0
 
LVL 3

Author Comment

by:lopem
ID: 2804954
Thanks Raidos and Lischke...

Just tell me something Lischke, how I can assign a jpg a bitmap? I tried this, but didnt work (from the delphi 5 help file):

var
 BitMap1,BitMap2 : TBitMap;
 MyFormat : Word;
begin
   BitMap2 := TBitMap.Create;
   BitMap1 := TBitMap.Create;
try
   BitMap1.LoadFromFile('c:\images\image001.jpg');
   BitMap2.Assign(BitMap1);     // Copy BitMap1 into BitMap2
   BitMap2.Dormant;             // Free up GDI resources
   BitMap2.FreeImage;           // Free up Memory.
   Canvas.Draw(20,20,BitMap2);  // Note that previous calls don't lose the image

   BitMap2.Monochrome := true;
   Canvas.Draw(80,80,BitMap2);
   BitMap2.ReleaseHandle;       // This will actually lose the bitmap;
 finally
   BitMap1.Free;
   BitMap2.Free;
 end;
end;

Can you send a simple example of loading a jpeg, do a bitmap assign so I can use its canvas?

Thanks a lot,
Manuel
0
 
LVL 10

Accepted Solution

by:
Lischke earned 30 total points
ID: 2805003
This is wrong because you cannot load a jpg with a TBitmap. The easiest way is to use the polymorphic abilitiy of TPicture.

  Pic := TPicture.Create;
  Pic.LoadFromFile('MyImage.jpg');
  Form1.Image1.Picture.Bitmap.Assign(Pic.Graphic);
  Pic.Free;

Now it is safe to access the bitmap's canvas of Image1.

Ciao, Mike
0
 
LVL 3

Author Comment

by:lopem
ID: 2806374
Just fot the sake of completeness... To use Lischke's idea you have to declare the unit jpeg in the uses section...

Best regards,
Manuel López (lopem)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Not only does Prezi allow you to create non-linear presentations, it also makes it easy to apply transition animations between your frames. Learn how to apply a fade-in transition to select items and entire frames in this tutorial Select "Edit Path"…

839 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