我无法将GDI +位图转换为C ++中的base 64
我到目前为止:
编写了您可以在下面看到的功能。
称为它,传入我知道有数据的位图。我知道位图具有数据,因为我正在此函数之后直接调用的另一个函数中使用。
问题是charPixels数组始终充满零。
第二,请您向我解释解锁钻头的安全方法是什么。我担心,如果我只是在最后才做,如果那时还没有实际锁定,我会得到一个例外。
Stride是一个正数,它是1280。
我也可以使用'finally',因为它是C ++的MS扩展。
[[注意:代码已更新,因为我错误地粘贴了错误的代码]
std::string GdiBitmapToBase64(Gdiplus::Bitmap* gdiBitmap, int width, int height) { unsigned char* charPixels = nullptr; try { Gdiplus::Rect rect = Gdiplus::Rect(0, 0, width, height); Gdiplus::BitmapData gdiBitmapData; gdiBitmap->LockBits(&rect, Gdiplus::ImageLockMode::ImageLockModeRead, PixelFormat32bppARGB, &gdiBitmapData); auto stride = gdiBitmapData.Stride; if (stride < 0) stride = -stride; charPixels = new unsigned char[height * stride]; memset(charPixels, 0, height * stride); memcpy(charPixels, gdiBitmapData.Scan0, stride); std::string ret = base64_encode(charPixels, stride); gdiBitmap->UnlockBits(&gdiBitmapData); return ret; } finally { if(charPixels != nullptr) { delete[] charPixels; } } }
这里是调用此方法的代码。这可能会有所帮助:
void CLIScumm::Wrapper::ScreenUpdated(const void* buf, int pitch, int x, int y, int w, int h, PalletteColor* color) { const unsigned char* bufCounter = static_cast<const unsigned char*>(buf); for (int hightCounter = 0; hightCounter < h; hightCounter++, bufCounter = bufCounter + pitch) { for (int widthCounter = 0; widthCounter < w; widthCounter++) { PalletteColor currentColor = *(color + *(bufCounter + widthCounter)); gdiBitmap->SetPixel(x + widthCounter, y + hightCounter, Gdiplus::Color(currentColor.r, currentColor.g, currentColor.b)); } } _screenUpdated->Invoke(gcnew System::String(GdiBitmapToBase64(gdiBitmap, DISPLAY_DEFAULT_WIDTH, DISPLAY_DEFAULT_HEIGHT).c_str())); }
和声明:
namespace CLIScumm {
public ref class Wrapper {
...
private:
...
Gdiplus::Graphics* gdiGraphics;
Gdiplus::Bitmap* gdiBitmap;
...
};
And the initialization:
void CLIScumm::Wrapper::init()
{
if (!hasStarted)
{
try
{
if (!hasStarted)
{
...
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
(malloc(sizeof(100000) * DISPLAY_DEFAULT_HEIGHT * DISPLAY_DEFAULT_WIDTH));
gdiBitmap = new Gdiplus::Bitmap(DISPLAY_DEFAULT_WIDTH, DISPLAY_DEFAULT_HEIGHT, PixelFormat32bppARGB);
gdiGraphics = new Gdiplus::Graphics(gdiBitmap);
InitImage();
...
}
}
...
}
}
我到目前为止尚无法将GDI +位图转换为C ++中的base 64:下载并包含此库:https://github.com/ReneNyffenegger/cpp-base64编写了可以在下面看到的函数。 ...
谢谢大家的帮助,但我解决了我自己的问题。