?
Solved

rgb colour values of a jpeg image?

Posted on 2000-05-11
5
Medium Priority
?
567 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
[X]
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
  • 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 90 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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