添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Why Gdiplus::Graphics object assigned to the pointer data member in the constructor does not draw on TImage or TPaintBox objects?

Ask Question

I don't understand why the Gdiplus::Graphics object assigned to the pointer data member in the constructor draws on the TForm object, but does not draw on the TImage or TPaintBox objects? However, the Gdiplus::Graphics object draws well on TImage or TPaintBox objects provided that the Gdiplus::Graphics object was assigned to the pointer data member before calling Gdiplus::Graphics functions for drawing.

Here is the code:

__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
    Gdiplus::GdiplusStartupInput GdiplusStartupInput;
    Gdiplus::GdiplusStartup(&mGdiplusToken, &GdiplusStartupInput, NULL);
    // If the Gdiplus::Graphics object is created and assigned to pointer data member here it draws on TForm but does not draw on //TImage or TPaintBox
    //  mGraph = new Gdiplus::Graphics{Image1->Canvas->Handle};
void __fastcall TForm2::Button1Click(TObject *Sender)
    Gdiplus::Status Status;
    Gdiplus::Color Red {Gdiplus::Color::Red};
    Gdiplus::Color Blue {Gdiplus::Color::Blue};
    Gdiplus::Color Green {Gdiplus::Color::Green};
    Gdiplus::Pen RedPen{Red, 2};
    Gdiplus::Pen BluePen{Blue, 2};
    Gdiplus::Pen GreenPen{Green, 2};
    Gdiplus::Graphics Graph1{Image1->Canvas->Handle};
    Gdiplus::Graphics* Graph2 = new Gdiplus::Graphics{Image1->Canvas->Handle};
    Status = Graph1.DrawLine(&RedPen, 50, 50, 100, 50);
    Status = Graph2->DrawLine(&BluePen, 50, 100, 100, 100);
    //If Gdiplus::Graphics object is created and assigned to the pointer data member here it draws on TForm or TImage or TPaintBox 
    mGraph = new Gdiplus::Graphics{Image1->Canvas->Handle};
    Status = mGraph->DrawLine(&GreenPen, 50, 150, 100, 150);
    Image1->Repaint();
                I think the question may be improved showing where mGraph is declared and instantiated and indicating in the code the lines that don't work as you expect.
– MatG
                May 23, 2021 at 14:34
                Does Image1 actually hold a non-empty TBitmap at the time you request its Handle? If not, then the TImage will create a blank bitmap.  Gdi+ won't resize the bitmap, only draw on it.  So, if the bitmap is empty, there is nothing for Gdi+ to draw on. So make sure the bitmap has the required dimensions beforehand.
– Remy Lebeau
                May 23, 2021 at 16:25
                On a side note, your Button1Click() is leaking Graph2. You need to add a call to delete Graph2; when you are done using Graph2.
– Remy Lebeau
                May 23, 2021 at 16:28
                @RemyLebeau Thank you. Image1 was created at design time.  Does it hold a blank bitmap in such a case? What is the definition of a blank bitmap?  Dimensions equal to zero?
– maksim_volodin
                May 23, 2021 at 16:58
                @RemyLebeau Why does it not needed to adjust the bitmap sizes when I have the statement mGraph = new Gdiplus::Graphics{Image1->Canvas->Handle}; not in the constructor but in the event handler or any other member function?
– maksim_volodin
                May 23, 2021 at 17:06
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.