| 
 | 
 
 
发表于 2024-11-21 12:47:26
|
显示全部楼层
 
 
 
 
 
访问冲突通常是由于对无效指针或内存的操作引起的。在使用 GDI+ 时,尤其需要注意以下几点: 
 
1. **初始化 GDI+**:确保在使用 GDI+ 之前正确初始化,它通常在程序启动时进行。确保 `GdiplusStartup` 的返回值表示成功。 
 
2. **检查图像路径**:确认输入路径和输出路径正确,并且输入图像文件存在。 
 
3. **指针正确性**:在使用指针的地方,确保所有指针都被正确初始化且指向有效的内存。 
 
4. **图像格式**:确保你对于图像的操作是支持的格式,比如说 BMP, PNG, JPEG 等。 
 
以下是一个完整的代码进行检查,同时增加一些守护代码: 
 
- #include <afxwin.h>
 
 - #include <GdiPlus.h>
 
 - using namespace Gdiplus;
 
 - #pragma comment (lib,"Gdiplus.lib")
 
  
- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
 
  
- void ChangeImageDPI(const CString& inputPath, const CString& outputPath, int newDPI) {
 
 -     GdiplusStartupInput gdiplusStartupInput;
 
 -     ULONG_PTR gdiplusToken;
 
 -     if (GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != Ok) {
 
 -         AfxMessageBox(_T("Failed to initialize GDI+"));
 
 -         return;
 
 -     }
 
  
-     Bitmap* image = new Bitmap(inputPath);
 
 -     if (image->GetLastStatus() != Ok) {
 
 -         AfxMessageBox(_T("Failed to load image"));
 
 -         delete image;
 
 -         GdiplusShutdown(gdiplusToken);
 
 -         return;
 
 -     }
 
  
-     UINT originalWidth = image->GetWidth();
 
 -     UINT originalHeight = image->GetHeight();
 
 -     float scaleFactor = newDPI / 96.0f;
 
 -     UINT newWidth = static_cast<UINT>(originalWidth / scaleFactor);
 
 -     UINT newHeight = static_cast<UINT>(originalHeight / scaleFactor);
 
  
-     Bitmap* newImage = new Bitmap(newWidth, newHeight, image->GetPixelFormat());
 
 -     if (newImage->GetLastStatus() != Ok) {
 
 -         AfxMessageBox(_T("Failed to create new image"));
 
 -         delete image;
 
 -         delete newImage;
 
 -         GdiplusShutdown(gdiplusToken);
 
 -         return;
 
 -     }
 
  
-     newImage->SetResolution(static_cast<REAL>(newDPI), static_cast<REAL>(newDPI));
 
  
-     Graphics graphics(newImage);
 
 -     if (graphics.DrawImage(image, 0, 0, newWidth, newHeight) != Ok) {
 
 -         AfxMessageBox(_T("Failed to draw image"));
 
 -         delete image;
 
 -         delete newImage;
 
 -         GdiplusShutdown(gdiplusToken);
 
 -         return;
 
 -     }
 
  
-     CLSID pngClsid;
 
 -     if (GetEncoderClsid(L"image/png", &pngClsid) == -1) {
 
 -         AfxMessageBox(_T("Failed to get image encoder"));
 
 -     } else if (newImage->Save(outputPath, &pngClsid, NULL) != Ok) {
 
 -         AfxMessageBox(_T("Failed to save image"));
 
 -     }
 
  
-     delete image;
 
 -     delete newImage;
 
 -     GdiplusShutdown(gdiplusToken);
 
 - }
 
  
- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
 
 -     UINT num = 0;
 
 -     UINT size = 0;
 
 -     GetImageEncodersSize(&num, &size);
 
 -     if (size == 0) return -1;
 
  
-     ImageCodecInfo* 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;
 
 - }
 
  复制代码 
 
**建议**: 
- 检查代码的所有路径(特别是确保输入图像存在)。 
- 检查您使用的图像格式是否受支持。 
- 使用异常处理(`try/catch`)来捕获可能出现的异常。  
 
以上这些建议和代码可以帮助您定位并解决访问冲突问题。如果仍然存在问题,还请检查环境配置或者其他依赖库的版本问题。 
 
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |   
 
 
 
 |