wxStaticBitmap
我在 wxFormBuilder 制作的表单上添加了一个:
show_image = new wxStaticBitmap( this, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0 );
如何使用 C++ 和 Curl 在其上显示基于 Internet 的图像?例如,像这样的图像:
wxStaticBitmap
我在 wxFormBuilder 制作的表单上添加了一个:
show_image = new wxStaticBitmap( this, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0 );
如何使用 C++ 和 Curl 在其上显示基于 Internet 的图像?例如,像这样的图像:
#include <string>
#include <iostream>
#include <wx/string.h>
#include <wx/image.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t read_pos;
size_t size;
};
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
wxImage *DownloadImage(string image_url)
{
wxImage *pImg = NULL;
struct MemoryStruct chunk;
CURL* curlCtx = curl_easy_init();
curl_easy_setopt(curlCtx, CURLOPT_URL, image_url.c_str());
curl_easy_setopt(curlCtx, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curlCtx, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curlCtx, CURLOPT_FOLLOWLOCATION, 1);
chunk.memory=NULL;
chunk.size = 0;
CURLcode rc = curl_easy_perform(curlCtx);
if (rc)
{
printf("!!! Failed to download\n");
}
wxMemoryInputStream *memin = new wxMemoryInputStream(chunk.memory, chunk.size);
if((pImg = new wxImage()) != NULL){
if(!pImg->LoadFile(*memin, wxBITMAP_TYPE_JPEG)){
delete pImg;
pImg = NULL;
}
}
curl_easy_cleanup(curlCtx);
return pImg;
}
wxImage imagewx = *DownloadImage("https://www.google.com/images/srpr/logo4w.png");
if(imagewx.IsOk()){
show_image->SetBitmap(wxBitmap(imagewx));
}
我不确定那是正确的方法,但它对我有用