欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ GDI實現(xiàn)圖片格式轉(zhuǎn)換

 更新時間:2023年12月15日 15:14:56   作者:XXYBMOOO  
GDI+(Graphics Device Interface Plus)是一種用于圖形繪制和圖像處理的應用程序編程接口(API),在Windows平臺上廣泛使用,本文就來介紹一下如何使用GDI實現(xiàn)圖片格式轉(zhuǎn)換吧

GDI+(Graphics Device Interface Plus)是一種用于圖形繪制和圖像處理的應用程序編程接口(API),在Windows平臺上廣泛使用。在GDI+中,可以使用Bitmap類來加載、保存和處理圖像。

要進行圖像格式轉(zhuǎn)換,需要加載源圖像并創(chuàng)建一個新的目標圖像,然后使用GDI+提供的方法將源圖像的像素數(shù)據(jù)復制到目標圖像中。以下是一個詳細的步驟解釋:

1.引入GDI+庫:在使用GDI+之前,需要引入相應的GDI+庫,通常是gdiplus.dll。

2.初始化GDI+:在使用GDI+之前,需要先初始化GDI+庫。在開始使用GDI+之前,調(diào)用GdiplusStartup函數(shù)來初始化GDI+。在處理完圖像后,應調(diào)用GdiplusShutdown函數(shù)來釋放GDI+資源。

#include <windows.h>
#include <gdiplus.h>
 
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
 
int main()
{
    // 初始化GDI+
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
    // 進行圖像處理操作
    
    // 關(guān)閉GDI+
    Gdiplus::GdiplusShutdown(gdiplusToken);
    
    return 0;
}

3.加載源圖像:使用Bitmap類的構(gòu)造函數(shù)或Bitmap::FromFile方法加載源圖像。例如,可以使用以下代碼加載一個名為input.jpg的JPEG圖像:

Gdiplus::Bitmap* sourceImage = Gdiplus::Bitmap::FromFile(L"input.jpg");

4.創(chuàng)建目標圖像:創(chuàng)建一個空的目標圖像,使用Bitmap類的構(gòu)造函數(shù)或Bitmap::Clone方法。目標圖像的大小和像素格式應根據(jù)需要進行設置。例如,可以使用以下代碼創(chuàng)建一個與源圖像相同大小和像素格式的新圖像:

Gdiplus::Bitmap* targetImage = new Gdiplus::Bitmap(sourceImage->GetWidth(), sourceImage->GetHeight(), sourceImage->GetPixelFormat());

5.執(zhí)行圖像格式轉(zhuǎn)換:使用Graphics類和DrawImage方法將源圖像的像素數(shù)據(jù)復制到目標圖像中。DrawImage方法可以接受多種不同的參數(shù)組合,以實現(xiàn)不同的繪制和轉(zhuǎn)換效果。以下是一個示例,將源圖像完全復制到目標圖像中:

Gdiplus::Graphics graphics(targetImage);
graphics.DrawImage(sourceImage, 0, 0, sourceImage->GetWidth(), sourceImage->GetHeight());

6.保存目標圖像:使用Bitmap::Save方法將目標圖像保存到磁盤文件或內(nèi)存流中??梢灾付ㄋ璧膱D像格式和保存選項。例如,可以使用以下代碼將目標圖像保存為名為output.png的PNG圖像:

targetImage->Save(L"output.png", Gdiplus::ImageFormatPNG);

7.釋放資源:在完成圖像處理后,需要釋放所分配的內(nèi)存。使用delete運算符釋放源圖像和目標圖像的內(nèi)存。

delete sourceImage;
delete targetImage;

以上是使用GDI+進行圖像格式轉(zhuǎn)換的一般步驟。請注意,這只是一個概述,并且在實際應用中可能需要處理更多的細節(jié)和錯誤檢查。確保正確處理錯誤和異常情況,以及適當釋放資源,以避免內(nèi)存泄漏和其他問題。

完整示例代碼

#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
#include <string>
using namespace Gdiplus;
 
#pragma comment(lib,"gdiplus")
 
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;
 
    // Get the number of image encoders and the size of the array
    GetImageEncodersSize(&num, &size);
    if (size == 0)
        return -1;  // Failure
 
    // Allocate memory for the image encoder array
    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if (pImageCodecInfo == NULL)
        return -1;  // Failure
 
    // Get all image encoders
    GetImageEncoders(num, size, pImageCodecInfo);
 
    // Find the image encoder that matches the specified format
    for (UINT j = 0; j < num; ++j)
    {
        if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
        {
            *pClsid = pImageCodecInfo[j].Clsid;
            free(pImageCodecInfo);
            return j;  // Success
        }
    }
 
    // Free the allocated memory
    free(pImageCodecInfo);
    return -1;  // Failure
}
 
bool ConvertImageFormatFromMemory(const char* imageData, ULONG imageDataSize, const std::string& outputFilePath, const wchar_t* outputFormat)
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
 
    // Initialize GDI+
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
 
    CLSID encoderClsid;
    Status stat;
 
    // Create a stream from the image data
    IStream* pStream = NULL;
    CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    pStream->Write(imageData, imageDataSize, NULL);
    pStream->Seek({ 0 }, STREAM_SEEK_SET, NULL);
 
    // Load the image from the stream
    Bitmap* bitmap = new Bitmap(pStream, FALSE);
    Image* image = static_cast<Image*>(bitmap);
 
    // Get the CLSID of the output format encoder
    GetEncoderClsid(outputFormat, &encoderClsid);
 
    // Convert the output file path to wide-character string
    int wideCharLen = MultiByteToWideChar(CP_UTF8, 0, outputFilePath.c_str(), -1, NULL, 0);
    wchar_t* wideCharPath = new wchar_t[wideCharLen];
    MultiByteToWideChar(CP_UTF8, 0, outputFilePath.c_str(), -1, wideCharPath, wideCharLen);
 
    // Save the image in the desired format
    stat = image->Save(wideCharPath, &encoderClsid, NULL);
 
    // Clean up
    delete image;
    pStream->Release();
    GdiplusShutdown(gdiplusToken);
    delete[] wideCharPath;
 
    return (stat == Ok);
}
 
int main()
{
    // Assuming you have the BMP image data in a `char*` buffer named `imageData`
    char* imageData = /* Your BMP image data */;
    ULONG imageDataSize = /* Size of the BMP image data */;
 
    const std::string outputFilePath = "output.jpg";
    const wchar_t* outputFormat = L"image/jpeg";
 
    bool success = ConvertImageFormatFromMemory(imageData, imageDataSize, outputFilePath, outputFormat);
 
    if (success)
        printf("Image saved successfully.\n");
    else
        printf("Failed to save image.\n");
 
    return 0;
}

到此這篇關(guān)于C++ GDI實現(xiàn)圖片格式轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)C++圖片格式轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Qt使用QCustomPlot的實現(xiàn)示例

    Qt使用QCustomPlot的實現(xiàn)示例

    QCustomPlot是一個基于Qt C++的圖形庫,用于繪制和數(shù)據(jù)可視化,并為實時可視化應用程序提供高性能服務,本文主要介紹了Qt使用QCustomPlot的實現(xiàn)示例,感興趣的可以了解一下
    2024-01-01
  • 詳解QT使用QtGui顯示QImage的幾種方法

    詳解QT使用QtGui顯示QImage的幾種方法

    本文主要介紹了QT使用QtGui顯示QImage的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-06-06
  • C++如何去除cpp文件的注釋詳解

    C++如何去除cpp文件的注釋詳解

    在日常工作中,我們會給c/c++代碼寫上一些注釋,但是往往為了保持最終的代碼盡可能小,我們需要刪除注釋,手動刪除太緩慢了,下面這篇文章主要給大家介紹了關(guān)于C++如何去除cpp文件注釋的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • c++超細致講解引用

    c++超細致講解引用

    在我們?nèi)粘5纳钪忻總€人都或多或少存在一個"外號",例如《西游記》中孫悟空就有諸多外號:美猴王,孫行者,齊天大圣等等。那么在C++中,也可以給一個已經(jīng)存在的變量取別名,這就是引用。那么接下來深入來探討一下引用
    2022-05-05
  • C++冒泡排序與選擇排序詳解

    C++冒泡排序與選擇排序詳解

    大家好,本篇文章主要講的是C++冒泡排序與選擇排序詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 使用C語言中的time函數(shù)獲取系統(tǒng)時間

    使用C語言中的time函數(shù)獲取系統(tǒng)時間

    在C語言中可以使用time函數(shù)來獲取系統(tǒng)時間,以下對time函數(shù)進行了介紹,需要的朋友可以過來參考下
    2013-07-07
  • C語言圖文并茂講解分支語句用法

    C語言圖文并茂講解分支語句用法

    分支結(jié)構(gòu)的執(zhí)行是依據(jù)一定的條件選擇執(zhí)行路徑,而不是嚴格按照語句出現(xiàn)的物理順序。分支結(jié)構(gòu)的程序設計方法的關(guān)鍵在于構(gòu)造合適的分支條件和分析程序流程,根據(jù)不同的程序流程選擇適當?shù)姆种дZ句
    2022-04-04
  • C++超詳細講解運算符重載

    C++超詳細講解運算符重載

    本文包括了對C++類的6個默認成員函數(shù)中的賦值運算符重載和取地址和const對象取地址操作符的重載。運算符是程序中最最常見的操作,例如對于內(nèi)置類型的賦值我們直接使用=賦值即可,因為這些編譯器已經(jīng)幫我們做好了,但是對象的賦值呢?能直接賦值嗎
    2022-06-06
  • C++實現(xiàn)學生信息管理系統(tǒng)

    C++實現(xiàn)學生信息管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C++11?lambda(匿名函數(shù))表達式詳細介紹

    C++11?lambda(匿名函數(shù))表達式詳細介紹

    lambda 表達式(lambda expression)是一個匿名函數(shù),C++11中的lambda表達式用于定義并創(chuàng)建匿名的函數(shù)對象,以簡化編程工作,下面這篇文章主要給大家介紹了關(guān)于C++11?lambda(匿名函數(shù))表達式的相關(guān)資料,需要的朋友可以參考下
    2022-07-07

最新評論