E
E
Evgeny Petryaev2020-10-10 02:52:20
C++ / C#
Evgeny Petryaev, 2020-10-10 02:52:20

How to write unsigned char* array via stl?

I wrote through the << operator and through write in the first case there is very little 5 kb, in the second case it requires char * when cast, the file is not read correctly by the graphic editor

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question