Rectangle 27 0

Delphi TImage: How do I display images that are larger than the TImage component they are being loaded in or assigned to?


procedure TForm1.btnBrowseClick(Sender: TObject);
var
  jpg: TJPEGImage;
  bmp: TBitmap;
begin
  OpenPicturedialog1.InitialDir := FindImageFolder(true);
  if OpenPictureDialog1.Execute then
  begin
    Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);

    if Image1.Picture.Graphic is TJPEGImage then
    begin
      if TJPEGImage(Image1.Picture.Graphic).Grayscale then
      begin
        Image2.Picture.Assign(Image1.Picture.Graphic);
      end else
      begin
        jpg := TJPEGImage.Create;
        try
          jpg.Assign(Image1.Picture.Graphic);
          jpg.Grayscale := True;
          Image2.Picture.Assign(jpg);
        finally
          jpg.Free;
        end;
      end;
    end else
    begin
      bmp := TBitmap.Create;
      try
        bmp.PixelFormat := pf32bit;
        bmp.Assign(Image1.Picture.Graphic);
        ...
        Image2.Picture.Assign(bmp);
      finally
        bmp.Free;
      end;
    end;
  end;
end;

AutoSize is off by default. Only needed to check that option in the form image component and it worked as expected. Thx so much. Tomorrow, I'm going to rollin' the newer grayscale routine I was testing today, and our software rev will be complete. I can't believe I missed that checkbox. ps. .Assign(bmp) works as well as .Picture.LoadFromFile(name) with the checkbox set to [True]

Hi Remy... I was doing that first and it was doing the same thing, only on some pictures. So I retested that to verify, still blank. Pushed out zipped folder of the three images to a download folder for testing. I moved this code line, [ Image1.Picture.LoadFromFile(name);], right to the top inside the try code and removed the [Image1.Picture.Assign(bmp)] to retest it.

I saw your comment from this post earlier [stackoverflow.com/questions/22291162/ and I wondered if there was a formatting change for sRGB vs. regular RGB files as there is in 24-bit vs. 32-bit files. If there is that and it isn't covered in the component or JPEG file, I could see where it would just display blank.

If I load the JPGs as-is using TImage.Picture.LoadFromFile(), they display fine only if the TImage.Stretch or TImage.Proportional property is True. If they are both set to False (the default), the images display blank.

If I load the JPGs into TJPEGImage first and then assign that to TBitmap, the conversion itself works fine (proved by saving the TBitmap to a .bmp file), and the bitmaps are displayed fine only if Stretch or Proportional are True, otherwise they display blank.

Since you are not getting an error when loading the JPEG image, I suspect the problem may be in the conversion from TJPEGImage to TBitmap. Unless you are going to be manipulating the image pixels, you don't need the conversion at all, you can assign the original TJPEGImage directly to the TImage. The VCL's TPicture class uses derived TGraphic classes so it can display images in their native formats, you should utilize that feature, eg:

So, I think TCanvas is likely running into memory issues trying to display images at such larger dimensions. It has nothing to do with the particular type of JPGs being used (which are being loaded fine).

So, as another test, I tried using TPaintBox, drawing the images manually in its OnPaint event. If I use TCanvas.Draw() to draw the images as-is in their full dimensions, they display empty. If I use TCanvas.StretchDraw() to fit the images within the `PaintBox's client area, they display fine.

Update: I did some tests in XE2 on Windows 7, and saw the same issue you do.

Update: nevermind what I said above. I see that the provided JPGs just have really large DPIs and happen to all be white in their top-left corners. If the TImage/TPaintBox is sized large to compensate, I can now see the images in their full dimensions (it is easier to see if the TImage is placed on a TScrollBox with TImage.AutoSize set to True). That would explain why stretch/proportional drawing works, by stretching the images to match TImage's dimensions when needed.

thx for the update, I tried the stretchDraw and scrollbox earlier, but I may have missed the Autosize option. I'll give that a go.

Note