MFC读写图片(不占用)---------转

#include "stdafx.h"

#include <windows.h>

#include <gdiplus.h>

#include <stdio.h>

using namespace Gdiplus;

#pragma comment(lib, "gdiplus.lib")  

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)

{

    UINT num= 0;

    UINT size= 0;

    ImageCodecInfo* pImageCodecInfo= NULL;

    GetImageEncodersSize(&num, &size);

    if(size== 0)

    {

        return -1;

    }

    pImageCodecInfo= (ImageCodecInfo*)(malloc(size));

    if(pImageCodecInfo== NULL)

    {

        return -1;

    }

    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;

        }

    }

    free(pImageCodecInfo);

    return -1;

}

// 从内存加载图片,失败返回NULL

Bitmap* LoadBitmapFromMemory(const void* memory, DWORD size)

{

    Bitmap* bmp = NULL;

    IStream* stream = NULL;

    if (CreateStreamOnHGlobal(NULL, TRUE, &stream) == S_OK)

    {

        ULARGE_INTEGER uli;

        uli.QuadPart = size;

        stream->SetSize(uli);

        if (stream->Write(memory, size, NULL) == S_OK)

            bmp = new Bitmap(stream);

        stream->Release();

    }

    return bmp;

}

// 从文件加载图片,不独占文件,失败返回NULL

Bitmap* LoadBitmapFromFile(const TCHAR* file_name)

{

    Bitmap* bmp = NULL;

    HANDLE file_handle = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (file_handle != INVALID_HANDLE_VALUE)

    {

        DWORD temp = 0;

        DWORD file_size = GetFileSize(file_handle, &temp);

        if (file_size && !temp)  // 不处理大于4G的文件

        {

            // 将图片文件读到内存后,再从内存创建Bitmap

            unsigned char* buffer = new unsigned char[file_size];

            if (ReadFile(file_handle, buffer, file_size, &temp, NULL))

                bmp = LoadBitmapFromMemory(buffer, temp);

            delete [] buffer;

        }

        CloseHandle(file_handle);

    }

    return bmp;

}

int _tmain(int argc, _TCHAR* argv[])

{

    GdiplusStartupInput gdiplusStartupInput;

    ULONG_PTR gdiplusToken;

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);//开启GID+,一定要开启,不然图片读取不了

    Status stat;

    CLSID  clsid;

    char   propertyValue[] = "Fake Photograph";

     Bitmap* bitmap = LoadBitmapFromFile(L"E:/sandbox/stone.jpg");

    PropertyItem* propertyItem = new PropertyItem;

    // Get the CLSID of the JPEG encoder.

    GetEncoderClsid(L"image/jpeg", &clsid);

    propertyItem->id = PropertyTagCopyright;

    propertyItem->length = 16;  // string length including NULL terminator

    propertyItem->type = PropertyTagTypeASCII;

    propertyItem->value = propertyValue;

    bitmap->SetPropertyItem(propertyItem);

  

    stat = bitmap->Save(L"E:/sandbox/stone.jpg", &clsid, NULL);

    if(stat == Ok)

        printf("FakePhoto2.jpg saved successfully.\n");

    delete propertyItem;

    delete bitmap;

    GdiplusShutdown(gdiplusToken);

    return 0;

    return 0;

}

猜你喜欢

转载自blog.csdn.net/Hat_man_/article/details/110708240