Gremlin92
@Gremlin92
Целеустремленный

Как записать массив unsigned char* через stl?

Записывал через оператор << и через write в первом случае очень мало 5 кб, во втором случае требует char* при приведении файл не читается правильно редактором графическим
#include <iostream>
#include <WinSock.h>
#include <vector>
#include <fstream>
#include <ctime>
#pragma comment (lib,"WS2_32.lib")
#pragma warning(disable : 4996)
int main()
{
     HDC ScreenDC = GetDC(0);
            HDC MemoryDC = CreateCompatibleDC(ScreenDC);
            int ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
            int ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
            ScreenWidth = ((ScreenWidth - 1) / 4 + 1) * 4;
            BITMAPINFO BMI;
            BMI.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            BMI.bmiHeader.biWidth = ScreenWidth;
            BMI.bmiHeader.biHeight = ScreenHeight;
            BMI.bmiHeader.biSizeImage = ScreenWidth * ScreenHeight * 3;
            BMI.bmiHeader.biCompression = BI_RGB;
            BMI.bmiHeader.biBitCount = 24;
            BMI.bmiHeader.biPlanes = 1;
            DWORD ScreenshotSize;
            ScreenshotSize = BMI.bmiHeader.biSizeImage;
            unsigned char* ImageBuffer;
            HBITMAP hBitmap = CreateDIBSection(ScreenDC, &BMI, DIB_RGB_COLORS, (void**)&ImageBuffer, 0, 0);
            SelectObject(MemoryDC, hBitmap);
            BitBlt(MemoryDC, 0, 0, ScreenWidth, ScreenHeight, ScreenDC, 0, 0, SRCCOPY);
            DeleteDC(MemoryDC);
            ReleaseDC(NULL, ScreenDC);
   unsigned char tgaHeader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            unsigned char header[6];
            unsigned char tempColors = 0;
               header[0] = ScreenWidth % 256;
            header[1] = ScreenWidth / 256;
            header[2] = ScreenHeight % 256;
            header[3] = ScreenHeight / 256;
            header[4] = BMI.bmiHeader.biBitCount;
            header[5] = 0;
   std::string namescreenshot = "5.tga";
   std::ofstream out;          // поток для записи
            out.open(namescreenshot,std::ios::binary); // окрываем файл для записи
            if (out.is_open())
            {
                out << tgaHeader;
                out << header;
            }
   int count = BMI.bmiHeader.biSizeImage;
              if (out.is_open())
                out.write((char*)ImageBuffer,count);
            out.close();
return 0;
}
  • Вопрос задан
  • 221 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы