gpt4 book ai didi

c++ - 如何通过 C++ 在 wxWidgets 中显示来自互联网的图像?

转载 作者:行者123 更新时间:2023-11-28 07:23:25 25 4
gpt4 key购买 nike

我在 wxFormBuilder 制作的表单上添加了一个wxStaticBitmap:

show_image = new wxStaticBitmap( this, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0 );

如何使用 C++ 和 Curl 在其上显示基于 Internet 的图像?例如,像这样的图像:

https://www.google.com/images/srpr/logo4w.png

最佳答案

 #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));
}

我不确定那是正确的方法,但它对我有用

关于c++ - 如何通过 C++ 在 wxWidgets 中显示来自互联网的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com