首页 > 其他 > 详细

cximage 裁剪图片并背景透明

时间:2021-01-28 17:55:42      阅读:38      评论:0      收藏:0      [点我收藏+]
extern "C" _declspec(dllexport) bool __stdcall SaveImage2(const char* srcImage, const char* json, bool transparent)
{
    CxImage image;
    if (!image.Load(srcImage, CXIMAGE_FORMAT_TIF))
        return false;

    neb::CJsonObject oJson(json);

    int count = oJson.GetArraySize();
    for (int i = 0; i < count; i++)
    {
        int left, top, width, height;
        string path;
        oJson[i]["Region"].Get("Left", left);
        oJson[i]["Region"].Get("Top", top);
        oJson[i]["Region"].Get("Width", width);
        oJson[i]["Region"].Get("Height", height);
        oJson[i].Get("Path", path);

        int imageWidth = image.GetWidth();
        if (width > imageWidth - left)
            width = imageWidth - left;

        int imageHeight = image.GetHeight();
        if (height > imageHeight - top)
            height = imageHeight - top;

        CxImage subImage(imageWidth, imageHeight);
        
        RECT r;
        r.left = left;
        r.right = left + width;
        r.top = top;
        r.bottom = top + height;

        if (!image.Crop(r, &subImage))
            return false;

        if (transparent)
        {
            if (subImage.GetTransIndex() != 24)
            {
                subImage.IncreaseBpp(24);
            }
            subImage.SetTransIndex(0);
            int B = 255, G = 255, R = 255;
            RGBQUAD rgbTrans = { B, G, R, 0 };
            subImage.SetTransColor(rgbTrans);
        }

        if (!subImage.Save(path.c_str(), CXIMAGE_FORMAT_GIF))
            return false;
    }

    return true;
}

 

cximage 裁剪图片并背景透明

原文:https://www.cnblogs.com/nanfei/p/14340681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!