使用gdi跟gdi+进行图片无失真缩放

1、首先是gdi

        CImage imgTemp;
	imgTemp.Load(_T("D:\\zzq.bmp"));//加载图片

	HDC hDC = imgTemp.GetDC();

	CDC *pImageDC1 = CDC::FromHandle(hDC);
	CBitmap *bitmap1 = pImageDC1->GetCurrentBitmap();
	BITMAP bmpInfo;
	bitmap1->GetBitmap(&bmpInfo);

	CImage ResultImage;//缩放结果
	// 建立新的 CImage
	int StretchWidth = 640;//缩放宽度
	int StretchHeight = 480;//缩放高度
	ResultImage.Create(StretchWidth, StretchHeight, bmpInfo.bmBitsPixel);
	CDC* ResultImageDC = CDC::FromHandle(ResultImage.GetDC());

	ResultImageDC->SetStretchBltMode(HALFTONE); // 使用最高品質的方式
	::SetBrushOrgEx(ResultImageDC->m_hDC, 0, 0, NULL); // 調整 Brush 的起點

									
        // 把 pImage 畫到 ResultImage 上面
	StretchBlt(*ResultImageDC, 0, 0, StretchWidth, StretchHeight, *pImageDC1, 0, 0, imgTemp.GetWidth(), imgTemp.GetHeight(), SRCCOPY);
	// pImage->Draw(*ResultImageDC,0,0,StretchWidth,StretchHeight,0,0,pImage->GetWidth(),pImage->GetHeight());

	imgTemp.ReleaseDC();
	ResultImage.ReleaseDC();


	long b = ResultImage.Save(_T("D:\\zzq10.bmp"));//保存
	if (FAILED(b))
	{

		int c = GetLastError();
	}

1、第二是gdi+

需要引入头文件和库

#include 

using namespace Gdiplus;

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

实现代码如下

        GdiplusStartupInput m_gdiplusStartupInput;//使用 GDI+SDK
	ULONG_PTR m_pGdiToken;//使用 GDI+SDK
	GdiplusStartup(&m_pGdiToken, &m_gdiplusStartupInput, NULL); //使用 GDI++SDK

	Bitmap* bt = new Bitmap(_T("D:\\zzq.bmp"));//打开文件
	//HBITMAP a = nullptr;
	//Bitmap bt1(a,nullptr);
	//bt1.FromHBITMAP(a);
	/*HBITMAP hBitmap = NULL;
	Color clr(0, 0, 0, 0);
	if (Ok == bt1.GetHBITMAP(clr, &hBitmap))
	{

	}*/
	////Bitmap srcBmp(srcWidth, srcHeight, srcWidth * 4, PixelFormat32bppARGB, pSrcData); //以解码后的rgb数据“pSrcData”创建位图,为ARGB格式,32位(也可以为24位RGB格式,根据具体需求)
	int destWidth = 480;
	int destHeight = 360;
	Bitmap* pDestBmp = new Bitmap(destWidth, destHeight, PixelFormat32bppARGB);

	//Image (destWidth, destHeight, PixelFormat32bppARGB); //新建缩放后的位图
	//Graphics* g1;
	//Image* pDestBmp1 = new Image(destWidth, destHeight);
	//Gdiplus::Bitmap* pDestBmp = new Gdiplus::Bitmap(destWidth, destHeight, bt.GetPixelFormat())
	Graphics* g = Graphics::FromImage(pDestBmp);
	g->SetInterpolationMode(InterpolationModeHighQualityBicubic);

	g->DrawImage(bt, 0, 0, destWidth, destHeight);


	SaveAsImageFile(pDestBmp, _T("D:\\zzq9.bmp"), L"image/bmp");

	delete g;
	g = nullptr;
	delete pDestBmp;
	pDestBmp = nullptr;
	delete bt;
	bt = nullptr;
	GdiplusShutdown(m_pGdiToken);

注意在debug下编译gdi+代码会报错 可以将以下代码注释掉

//#ifdef _DEBUG
//#define new DEBUG_NEW
//#endif

第三种直接用ffmpeg.exe进行缩放命令如下

ffmpeg.exe  -i zzq.bmp -vf "scale=524:524/a"  zzq16.bmp   

根据宽度等比例缩放

ffmpeg.exe  -i zzq.bmp -s 640x480  zzq16.bmp  

 

 

 

你可能感兴趣的:(无失真,缩放,gdi)