0

I have a JPG resource in my MFC application. I try to load it like this in OnInitDialog:

m_imgSplash.LoadFromResource(AfxGetResourceHandle(), IDR_SPLASHSCREEN);

Then, in the OnPaint handler I do the following:

void CSplashDlg::OnPaint() 
{
    CPaintDC dc(this); // device context for painting

    m_imgSplash.Draw(dc.GetSafeHdc(), m_rctSplash);
}

But I am not seeing my image ...

I saw this question but CResourceStream is unknown.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • What is your definition of `m_imgSplash`? – Adrian Mole Nov 20 '19 at 15:57
  • @Adrian-ReinstateMonica `CImage` – Andrew Truckle Nov 20 '19 at 15:58
  • OK, and how is your `IDR_SPLASHSCREEN` defined (in the .rc script)? – Adrian Mole Nov 20 '19 at 15:59
  • 1
    @Adrian-ReinstateMonica I am just trying this a moment. https://stackoverflow.com/a/10719437/2287576 – Andrew Truckle Nov 20 '19 at 16:00
  • 1
    `CImage::LoadFromResource` works for bitmap only. You can use `CPngImage` to load jpeg from resource. Your Jpeg resource has to be labeled as `PNG` type, like so: `IDI_JPEG1 PNG "file.jpg"`. Then attach the handle to `CImage` object, `m_image.Attach((HBITMAP)pngImage.Detach())` – Barmak Shemirani Nov 20 '19 at 20:33
  • @BarmakShemirani Does the `CPngImage` object have to exist for as long at the `CImage`? I can always convert my JPG to a PNG anyway and add it again as a new resource. – Andrew Truckle Nov 20 '19 at 22:38
  • 1
    `pngImage` is temporary. Basically `m_image.Attach((HBITMAP)pngImage.Detach())` will transfer the image to `m_image`, and you no longer need `CPngImage` – Barmak Shemirani Nov 20 '19 at 22:46
  • @BarmakShemirani It works. Much simpler code! Thanks. Question: what type of exception does the `Attach` throw since it is not documented. – Andrew Truckle Nov 20 '19 at 22:47
  • 1
    I don't know. Just make sure `pngImage.Load(...)` was successful, before calling `image.Attach((HBITMAP)png.Detach())`. You can also use `m_image.IsNull()` to make sure `m_image` is valid. – Barmak Shemirani Nov 20 '19 at 23:01

0 Answers0