使用cxImage庫生成縮略圖的步驟如下:
首先,你需要加載要生成縮略圖的原始圖像。
#include "cxImage.h"
CXIMAGE image;
if (!image.Load("path_to_your_image.jpg")) {
// 處理加載錯誤
return;
}
確定你想要的縮略圖的寬度和高度。
int thumbnailWidth = 100; // 縮略圖寬度
int thumbnailHeight = 100; // 縮略圖高度
使用cxImage::MakeThumbnail
方法來生成縮略圖。
CXIMAGE thumbnail;
if (!thumbnail.Create(thumbnailWidth, thumbnailHeight, 32)) {
// 處理創建錯誤
return;
}
// 將原始圖像的內容復制到縮略圖中
if (!image.BitBlt(thumbnail, 0, 0, thumbnailWidth, thumbnailHeight, CXIMAGE_FORMAT_RGB24)) {
// 處理復制錯誤
return;
}
將生成的縮略圖保存到文件中。
if (!thumbnail.Save("path_to_save_thumbnail.jpg")) {
// 處理保存錯誤
return;
}
以下是一個完整的示例代碼,展示了如何使用cxImage生成縮略圖:
#include "cxImage.h"
#include <iostream>
int main() {
CXIMAGE image;
if (!image.Load("path_to_your_image.jpg")) {
std::cerr << "Failed to load image." << std::endl;
return 1;
}
int thumbnailWidth = 100;
int thumbnailHeight = 100;
CXIMAGE thumbnail;
if (!thumbnail.Create(thumbnailWidth, thumbnailHeight, 32)) {
std::cerr << "Failed to create thumbnail." << std::endl;
return 1;
}
if (!image.BitBlt(thumbnail, 0, 0, thumbnailWidth, thumbnailHeight, CXIMAGE_FORMAT_RGB24)) {
std::cerr << "Failed to copy image data to thumbnail." << std::endl;
return 1;
}
if (!thumbnail.Save("path_to_save_thumbnail.jpg")) {
std::cerr << "Failed to save thumbnail." << std::endl;
return 1;
}
std::cout << "Thumbnail created successfully!" << std::endl;
return 0;
}
通過以上步驟,你可以輕松地使用cxImage庫生成縮略圖。