Paste / copy png to clipboard

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Paste / copy png to clipboard

Post by dkaip »

Hello.
I have a png image as string ..
std::string str(png_encoding.begin(), png_encoding.end());
Then copy to clipboard with ...

Code: Select all

  void toClipboard(const std::string& s) {
    CloseClipboard();
    OpenClipboard(0);
    EmptyClipboard();
    HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size());
    if (!hg) {
      CloseClipboard();
      return;
    }
    memcpy(GlobalLock(hg), s.c_str(), s.size());
    GlobalUnlock(hg);
    SetClipboardData(CF_TEXT, hg);
    CloseClipboard();
    GlobalFree(hg);
  }
Then i take string from clipboard ...

Code: Select all

wxString clip;
// Read some text
if (wxTheClipboard->Open())
{
    if (wxTheClipboard->IsSupported( wxDF_TEXT ))
    {
        wxTextDataObject data;
        wxTheClipboard->GetData( data );
        clip=( data.GetText() );
    }
    wxTheClipboard->Close();
}
vector<unsigned char> image(clip.begin(), clip.end());
Is this a good way? Now i want to convert vector<unsigned char> to wxBitmap.
Is any good function?
Thank's
Jim
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Paste / copy png to clipboard

Post by doublemax »

Why not use wxDF_BITMAP to transfer the bitmap directly?

The "dnd" sample contains code that shows how to use it.
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: Paste / copy png to clipboard

Post by dkaip »

All that is because i can not connect pdfium in windows with GCC, because of MSVC libraries.
So i bypass the pdfium lib with clipboard use, and there is no other way.
So i need the clipboard to make the transformation.
Than you.
Jim
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Paste / copy png to clipboard

Post by doublemax »

Create a wxMemoryInputStream from the binary data. Then you can pass that to wxImage::LoadFile()
https://docs.wxwidgets.org/trunk/classw ... tream.html
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Paste / copy png to clipboard

Post by PB »

BTW, placing anything into the Clipboard without the user doing it is usually a not so good idea. Users expect the Clipboard contain what they Copy/Cut into it and would be very surprised (and in some cases enraged) to not find it there....
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: Paste / copy png to clipboard

Post by dkaip »

Ok, but what other way exist to connect two exe files for transform data between exept clip and write and open files in disk(too slow process..)?
Thank you
Jim
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Paste / copy png to clipboard

Post by PB »

IMO, the clipboard is not a suitable choice for interprocess communication. There are other, better, tools for that. On wxWidgets side, see e.g. the IPC sample and the IPC overview.
Post Reply