源码资源网 发表于 2024-11-28 12:00:24

使用 GDI+ 库根据图像的有效像素来裁切图片,即把图片上下左右空白全部切除掉

使用 GDI+ 库根据图像的有效像素来裁切图片,即把图片上下左右空白全部切除掉
使用 GDI+ 库来处理图像裁剪。GDI+ 是 Windows 平台上用于图形处理的一个强大库,可以方便地处理图像,包括裁剪、缩放等操作。

以下是一个使用 GDI+ 库根据图像的有效像素裁剪图片的示例代码。此代码将去除图像上下左右的空白区域。

### 1. 包含必要的头文件

确保在你的源文件中包含 GDI+ 的头文件,并链接相应的库:

```cpp
#include <windows.h>
#include <gdiplus.h>
#include <iostream>

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

using namespace Gdiplus;
```


### 2. 初始化 GDI+

在 `main` 函数中初始化 GDI+:

```cpp
int main() {
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
   
    // 你的代码逻辑在这里

    GdiplusShutdown(gdiplusToken);
    return 0;
}
```


### 3. 裁剪图像的函数

以下是一个裁剪图像的示例函数:

```cpp
void CropImage(const WCHAR* inputFilePath, const WCHAR* outputFilePath) {
    // 加载图像
    Image image(inputFilePath);
    if (image.GetLastStatus() != Ok) {
      std::wcerr << L"无法加载图像: " << inputFilePath << std::endl;
      return;
    }

    int width = image.GetWidth();
    int height = image.GetHeight();

    // 查找有效像素的边界
    int left = width, right = 0, top = height, bottom = 0;

    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
            Color color;
            image.GetPixel(x, y, &color);

            // 假设白色为背景色,判断有效像素
            if (color.GetR() != 255 || color.GetG() != 255 || color.GetB() != 255) {
                if (x < left) left = x;
                if (x > right) right = x;
                if (y < top) top = y;
                if (y > bottom) bottom = y;
            }
      }
    }

    // 计算裁剪后的宽度和高度
    int newWidth = right - left + 1;
    int newHeight = bottom - top + 1;

    // 创建裁剪后的图像
    Bitmap croppedImage(newWidth, newHeight);
    Graphics g(&croppedImage);
    g.DrawImage(&image, 0, 0, left, top, newWidth, newHeight, UnitPixel);

    // 保存裁剪后的图像
    CLSID clsid;
    CLSIDFromString(L"{557C3B4D-1A2E-4C3D-BB2E-5C8A4B1A6D1D}", &clsid); // JPEG
    croppedImage.Save(outputFilePath, &clsid, NULL);
}
```


### 4. 在 `main` 函数中调用裁剪函数

```cpp
int main() {
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    const WCHAR* inputFilePath = L"input.jpg"; // 输入图像路径
    const WCHAR* outputFilePath = L"output.jpg"; // 输出裁剪后图像路径
    CropImage(inputFilePath, outputFilePath);

    GdiplusShutdown(gdiplusToken);
    return 0;
}
```


### 完整代码

以下是完整的代码示例:

```cpp
#include <windows.h>
#include <gdiplus.h>
#include <iostream>

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

using namespace Gdiplus;

void CropImage(const WCHAR* inputFilePath, const WCHAR* outputFilePath) {
    // 加载图像
    Image image(inputFilePath);
    if (image.GetLastStatus() != Ok) {
      std::wcerr << L"无法加载图像: " << inputFilePath << std::endl;
      return;
    }

    int width = image.GetWidth();
    int height = image.GetHeight();

    // 查找有效像素的边界
    int left = width, right = 0, top = height, bottom = 0;

    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
            Color color;
            image.GetPixel(x, y, &color);

            // 假设白色为背景色,判断有效像素
            if (color.GetR() != 255 || color.GetG() != 255 || color.GetB() != 255) {
                if (x < left) left = x;
                if (x > right) right = x;
                if (y < top) top = y;
                if (y > bottom) bottom = y;
            }
      }
    }

    // 计算裁剪后的宽度和高度
    int newWidth = right - left + 1;
    int newHeight = bottom - top + 1;

    // 创建裁剪后的图像
    Bitmap croppedImage(newWidth, newHeight);
    Graphics g(&croppedImage);
    g.DrawImage(&image, 0, 0, left, top, newWidth, newHeight, UnitPixel);

    // 保存裁剪后的图像
    CLSID clsid;
    CLSIDFromString(L"{557C3B4D-1A2E-4C3D-BB2E-5C8A4B1A6D1D}", &clsid); // JPEG
    croppedImage.Save(outputFilePath, &clsid, NULL);
}

int main() {
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    const WCHAR* inputFilePath = L"input.jpg"; // 输入图像路径
    const WCHAR* outputFilePath = L"output.jpg"; // 输出裁剪后图像路径
    CropImage(inputFilePath, outputFilePath);

    GdiplusShutdown(gdiplusToken);
    return 0;
}
```


### 注意事项

1. **背景色判断**:当前代码假设白色为背景色,可以根据实际情况调整判断条件。
2. **图像格式**:确保输入和输出的图像格式支持 GDI+。
3. **错误处理**:在实际应用中,建议添加更详细的错误处理逻辑。

通过以上代码,你可以实现根据图像的有效像素裁剪图片的功能。

页: [1]
查看完整版本: 使用 GDI+ 库根据图像的有效像素来裁切图片,即把图片上下左右空白全部切除掉

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。