zoukankan      html  css  js  c++  java
  • 为什么调用 GdiplusShutdown 函数会在 DllExports::GdipDeleteGraphics(nativeGraphics) 位置抛出异常?

    因为没有仔细看文档

    https://docs.microsoft.com/en-us/windows/desktop/api/Gdiplusinit/nf-gdiplusinit-gdiplusshutdown

    You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown.

    抛异常

    	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    	ULONG_PTR gdiplusToken;
    	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    	
    		HDC deskTop = GetDC(NULL);
    		int nBitPerPixel = GetDeviceCaps(deskTop, BITSPIXEL);
    		int nWidth = GetDeviceCaps(deskTop, HORZRES);
    		int nHeight = GetDeviceCaps(deskTop, VERTRES);
    		//int center
    		Gdiplus::Graphics graphics(deskTop);
    		graphics.TranslateTransform(nWidth / 2, nHeight / 2);
    		Gdiplus::Image *image = new Gdiplus::Image(L"D:\456.png");
    
    		printf("%dx%d BitPerPixel = %d
    ", nWidth, nHeight, nBitPerPixel);
    		graphics.DrawImage(image, -256 / 2, -256 / 2, 256, 256);
    
    		delete image;
    		image = nullptr;
    		ReleaseDC(NULL, deskTop);
    	
    	Gdiplus::GdiplusShutdown(gdiplusToken);
    

    使用 {} 大阔号把 gdi+ 的对象作用域分开,当离开作用域时,此时 gdi+ 对象的使用资源会释放掉,然后调用 Gdiplus::GdiplusShutdown(gdiplusToken); 函数就不会抛异常了。you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown.

    	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    	ULONG_PTR gdiplusToken;
    	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    	{
    		HDC deskTop = GetDC(NULL);
    		int nBitPerPixel = GetDeviceCaps(deskTop, BITSPIXEL);
    		int nWidth = GetDeviceCaps(deskTop, HORZRES);
    		int nHeight = GetDeviceCaps(deskTop, VERTRES);
    		//int center
    		Gdiplus::Graphics graphics(deskTop);
    		graphics.TranslateTransform(nWidth / 2, nHeight / 2);
    		Gdiplus::Image *image = new Gdiplus::Image(L"D:\456.png");
    
    		printf("%dx%d BitPerPixel = %d
    ", nWidth, nHeight, nBitPerPixel);
    		graphics.DrawImage(image, -256 / 2, -256 / 2, 256, 256);
    
    		delete image;
    		image = nullptr;
    		ReleaseDC(NULL, deskTop);
    	}
    	Gdiplus::GdiplusShutdown(gdiplusToken);
    
  • 相关阅读:
    .Net Core使用Options模式来使用配置项
    git忽略已经提交的文件(git忽略文件不起作用)
    AirTest
    VSCode搭建rust开发环境
    动态编译和加载java代码
    JavaScript动态应用代码(有点像Java里的drools)
    Win10 Rust 编译报错: linking with `link.exe` failed: exit code: 1181
    git 拉取仓库的单个目录
    dart里实现类似Java里的--classpath的功能
    Rust离线安装
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/9999167.html
Copyright © 2011-2022 走看看