在内存中创建GDI +位图,然后另存为png

问题描述 投票:3回答:2

我是C ++的新手,在使用GDI +库编写函数以在内存中创建新的位图时遇到了麻烦(所以不打开/读取现有的位图);然后绘制位图;在保存到png之前。特别是,我在创建位图和保存代码方面遇到了问题。我被限制使用代码块,即使我想,我也不能使用视觉工作室。代码如下:

#include "drawImage.h"
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace Gdiplus;

drawImage::drawImage(){}

void drawImage::DrawBitmap(int width, int height){
  GdiplusStartupInput gdiplusStartupInput;
  ULONG_PTR           gdiplusToken;
  GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

  {
    //Create a bitmap
    Bitmap myBitmap(width, height, PixelFormatCanonical);
    Graphics g(&myBitmap);
    Pen blackpen(Color(255,0,0,0), 3);

    //draw on bitmap
    int x1 = 1;
    int x2 = 200;
    int y1 = 1;
    int y2 = 200;
    g.DrawLine(&blackpen, x1,y1,x2,y2);

    // Save bitmap (as a png)
    CLSID pngClsid;
    GetEncoderClsid(L"image/png", &pngClsid);
    myBitmap.Save(L"C:\\test\\test.png", &pngClsid, NULL);
  }

  GdiplusShutdown(gdiplusToken);
}

我遇到的问题如下:

  1. “保存”代码无法编译,并提供错误消息“'GetEncoderClsid'未在此范围内声明”。但是,我从微软网站here直接得到了这个。我不认为这是转换为png的正确方法,但我不知道另一种方法吗?
  2. 编译并运行代码时(通过注释掉保存代码),它会在“Bitmap * myBitmap = new Bitmap(width,height,PixelFormatCanonical);”行中崩溃。并给出一条错误消息,说我的可执行文件已停止工作。

我添加了'gdi32'链接库以及'-lgdiplus'作为链接器选项。此外,我使用this website来帮助gdi的东西,虽然位图部分只涉及加载现有的位图(不在内存中创建新的位图)

我完全迷失了该怎么做,所以对此事的任何帮助或建议都非常感谢。

c++ codeblocks gdi+
2个回答
7
投票

核心问题是将错误的像素格式传递给Bitmap constructorPixelFormatCanonical不是受支持的pixel formats之一。这是一个用于确定像素格式是否规范的掩码(参见IsCanonicalPixelFormat)。你必须使用真正的像素格式,比如默认的PixelFormat32bppARGB

以下代码生成所需的输出:

首先,一个用于GDI +初始化的小助手类。这确保了在所有其他物体被摧毁之后执行d tor(即对GdiplusShutdown的调用)。关于破坏的顺序,它与OP中的附加范围具有相同的目的。此外,它还允许抛出异常。

#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#include <stdexcept>
using std::runtime_error;

struct GdiplusInit {
    GdiplusInit() {
        GdiplusStartupInput inp;
        GdiplusStartupOutput outp;
        if ( Ok != GdiplusStartup( &token_, &inp, &outp ) )
            throw runtime_error( "GdiplusStartup" );
    }
    ~GdiplusInit() {
        GdiplusShutdown( token_ );
    }
private:
    ULONG_PTR token_;
};

此代码取自MSDN示例Retrieving the Class Identifier for an Encoder

int GetEncoderClsid( const WCHAR* format, CLSID* pClsid )
{
    UINT  num = 0;          // number of image encoders
    UINT  size = 0;         // size of the image encoder array in bytes

    ImageCodecInfo* pImageCodecInfo = NULL;

    GetImageEncodersSize( &num, &size );
    if ( size == 0 )
        return -1;  // Failure

    pImageCodecInfo = (ImageCodecInfo*)( malloc( size ) );
    if ( pImageCodecInfo == NULL )
        return -1;  // Failure

    GetImageEncoders( num, size, pImageCodecInfo );

    for ( UINT j = 0; j < num; ++j )
    {
        if ( wcscmp( pImageCodecInfo[j].MimeType, format ) == 0 )
        {
            *pClsid = pImageCodecInfo[j].Clsid;
            free( pImageCodecInfo );
            return j;  // Success
        }
    }

    free( pImageCodecInfo );
    return -1;  // Failure
}

最后,GDI +渲染代码。它使用具有自动存储持续时间的对象,使其更紧凑和更安全。

void drawImage( int width, int height ) {
    GdiplusInit gdiplusinit;

    //Create a bitmap
    Bitmap myBitmap( width, height, PixelFormat32bppARGB );
    Graphics g( &myBitmap );
    Pen blackpen( Color( 255, 0, 0, 0 ), 3 );

    //draw on bitmap
    g.DrawLine( &blackpen, 1, 1, 200, 200 );

    // Save bitmap (as a png)
    CLSID pngClsid;
    int result = GetEncoderClsid( L"image/png", &pngClsid );
    if ( result == -1 )
        throw runtime_error( "GetEncoderClsid" );
    if ( Ok != myBitmap.Save( L"C:\\test\\test.png", &pngClsid, NULL ) )
        throw runtime_error( "Bitmap::Save" );
}

int main()
{
    drawImage( 200, 200 );
    return 0;
}

注意:看起来不需要GetEncoderClsid,因为那些是众所周知的常量。然而,试图将适当的WIC CLSIDCLSID_WICPngEncoder)传递给Bitmap::Save只会产生FileNotFound错误。


4
投票

对于1:GetEncoderClsid不是库函数,它是一个示例助手定义的here。如果要使用它,请复制站点中的代码。 (顺便说一句,你的链接明确说明了这一点。)

For 2。:您需要在创建任何GDI +对象或调用任何GDI +函​​数之前调用GdiplusStartup。请参阅其文档here

更准确地说GdiplusShutdown,因为这似乎让你遇到了新的问题:要求在调用GdiplusShutdown之前销毁所有GDI +对象。这意味着在调用pen之前,具有静态存储的对象(例如GdiplusShutdown)必须超出范围。如果你以现在的方式在DrawBitmap中调用它,情况就不是这样了。要么在GdiplusStartup中调用GdiplusShutdownmain,要么在{}GdiplusStartup之间的代码周围添加额外的括号GdiplusShutdown,因为这些引入了一个新的范围,当达到pen}将被销毁。

编辑:Nr。通过编辑在问题中修复了2。对于剩下的问题和代码的改进,请参阅@IInspectable的答案。

© www.soinside.com 2019 - 2024. All rights reserved.