Solved

rgb colour values of a jpeg image?

Posted on 2000-05-11
5
558 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export PDF for printing from InDesign 4 55
watermark on the graphic 9 81
Lite dgn, dwg free viewer 1 120
Unique identifier on a terminal server (rdp) 4 55
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this Micro Tutorial viewers will learn how to remove an unwanted object using Photoshop’s feature known as content-aware fill.
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.

803 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