Solved

rgb colour values of a jpeg image?

Posted on 2000-05-11
5
551 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
In this Micro Tutorial viewers will learn the basic shortcuts and functions of Illustrator. The viewer will learn about the paintbrush tool, anchor points, font sizing, and more.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…

743 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

12 Experts available now in Live!

Get 1:1 Help Now