• win32


    简单的demo:

    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    typedef struct
    {
        std::uint32_t biSize;
        std::int32_t  biWidth;
        std::int32_t  biHeight;
        std::uint16_t  biPlanes;
        std::uint16_t  biBitCount;
        std::uint32_t biCompression;
        std::uint32_t biSizeImage;
        std::int32_t  biXPelsPerMeter;
        std::int32_t  biYPelsPerMeter;
        std::uint32_t biClrUsed;
        std::uint32_t biClrImportant;
    } DIB;
    
    typedef struct
    {
        std::uint16_t type;
        std::uint32_t bfSize;
        std::uint32_t reserved;
        std::uint32_t offset;
    } HEADER;
    
    typedef struct
    {
        HEADER header;
        DIB dib;
    } BMP;
    
    
    int main()
    {
        std::cout << "Format Bitmap: " << IsClipboardFormatAvailable(CF_BITMAP) << "
    ";
        std::cout << "Format DIB: " << IsClipboardFormatAvailable(CF_DIB) << "
    ";
        std::cout << "Format DIBv5: " << IsClipboardFormatAvailable(CF_DIBV5) << "
    ";
    
        if (IsClipboardFormatAvailable(CF_BITMAP) || IsClipboardFormatAvailable(CF_DIB) || IsClipboardFormatAvailable(CF_DIBV5))
        {
            if (OpenClipboard(NULL))
            {
                HANDLE hClipboard = GetClipboardData(CF_DIB);
    
                if (!hClipboard)
                {
                    hClipboard = GetClipboardData(CF_DIBV5);
                }
    
                if (hClipboard != NULL && hClipboard != INVALID_HANDLE_VALUE)
                {
                    void* dib = GlobalLock(hClipboard);
    
                    if (dib)
                    {
                        DIB* info = reinterpret_cast<DIB*>(dib);
                        BMP bmp = { 0 };
                        bmp.header.type = 0x4D42;
                        bmp.header.offset = 54;
                        bmp.header.bfSize = info->biSizeImage + bmp.header.offset;
                        bmp.dib = *info;
    
                        std::cout << "Type: " << std::hex << bmp.header.type << std::dec << "
    ";
                        std::cout << "bfSize: " << bmp.header.bfSize << "
    ";
                        std::cout << "Reserved: " << bmp.header.reserved << "
    ";
                        std::cout << "Offset: " << bmp.header.offset << "
    ";
                        std::cout << "biSize: " << bmp.dib.biSize << "
    ";
                        std::cout << "Width: " << bmp.dib.biWidth << "
    ";
                        std::cout << "Height: " << bmp.dib.biHeight << "
    ";
                        std::cout << "Planes: " << bmp.dib.biPlanes << "
    ";
                        std::cout << "Bits: " << bmp.dib.biBitCount << "
    ";
                        std::cout << "Compression: " << bmp.dib.biCompression << "
    ";
                        std::cout << "Size: " << bmp.dib.biSizeImage << "
    ";
                        std::cout << "X-res: " << bmp.dib.biXPelsPerMeter << "
    ";
                        std::cout << "Y-res: " << bmp.dib.biYPelsPerMeter << "
    ";
                        std::cout << "ClrUsed: " << bmp.dib.biClrUsed << "
    ";
                        std::cout << "ClrImportant: " << bmp.dib.biClrImportant << "
    ";
    
                        std::ofstream file("C:/Users/strives/Desktop/Test.bmp", std::ios::out | std::ios::binary);
                        if (file)
                        {
                            file.write(reinterpret_cast<char*>(&bmp.header.type), sizeof(bmp.header.type));
                            file.write(reinterpret_cast<char*>(&bmp.header.bfSize), sizeof(bmp.header.bfSize));
                            file.write(reinterpret_cast<char*>(&bmp.header.reserved), sizeof(bmp.header.reserved));
                            file.write(reinterpret_cast<char*>(&bmp.header.offset), sizeof(bmp.header.offset));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biSize), sizeof(bmp.dib.biSize));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biWidth), sizeof(bmp.dib.biWidth));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biHeight), sizeof(bmp.dib.biHeight));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biPlanes), sizeof(bmp.dib.biPlanes));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biBitCount), sizeof(bmp.dib.biBitCount));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biCompression), sizeof(bmp.dib.biCompression));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biSizeImage), sizeof(bmp.dib.biSizeImage));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biXPelsPerMeter), sizeof(bmp.dib.biXPelsPerMeter));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biYPelsPerMeter), sizeof(bmp.dib.biYPelsPerMeter));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biClrUsed), sizeof(bmp.dib.biClrUsed));
                            file.write(reinterpret_cast<char*>(&bmp.dib.biClrImportant), sizeof(bmp.dib.biClrImportant));
                            file.write(reinterpret_cast<char*>(info + 1), bmp.dib.biSizeImage);
                        }
    
                        GlobalUnlock(dib);
                    }
                }
    
                CloseClipboard();
            }
        }
    
        return 0;
    }

    打开PAINT软件,拖一张bmp格式的图片,然后select all,再复制。 最后运行上面的代码。

  • 相关阅读:
    写了一个数据库的连继ID号(格式:xxxx000001)
    热心的网友<寒羽枫>帮忙解决水晶报表打印纸张问题
    解决vs2005自带水晶报表次数的限制的次数
    WebWork教程 Interceptor(拦截器)
    由于最近网站内容需要更新的还是满多的,于是想开发一个采集系统。收集了一下资料。
    ASP.NET AJAX 1.0 Beta 2 发布
    水晶报表的显示与打印不一至问题
    去年治疗过敏性鼻炎所用的药。
    正则表达式快速入门教程
    sql复制一条相同的记录最快最好的办法。
  • 原文地址:https://www.cnblogs.com/strive-sun/p/13757794.html
Copyright © 2020-2023  润新知