码迷,mamicode.com
首页 > 其他好文 > 详细

截取屏幕生成GIF

时间:2016-05-12 13:11:04      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

呃,一直想有一个这样功能的软件,但是貌似木有找到啊。

于是自己写了一个比较挫的。。

思路,截取ForegroundWindow图像生成png


void SavePictureAsPng(LPCTSTR lpFile)
{
	HWND hwnd = GetForegroundWindow();
	HDC hdcSrc = GetDC(hwnd);
	int nBitPerPixel = GetDeviceCaps(hdcSrc, BITSPIXEL);
	int nWidth = GetDeviceCaps(hdcSrc, HORZRES);
	int nHeight = GetDeviceCaps(hdcSrc, VERTRES);
	CImage image;
	image.Create(nWidth, nHeight, nBitPerPixel);
	BitBlt(image.GetDC(), 0, 0, nWidth, nHeight, hdcSrc, 0, 0, SRCCOPY);
	ReleaseDC(NULL, hdcSrc);
	image.ReleaseDC();
	image.Save(lpFile, Gdiplus::ImageFormatPNG);
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg = {0};
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_SCREENCAPTURE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SCREENCAPTURE));

	for (int i = 0; i < 10; i++)
	{
		TCHAR strFile[MAX_PATH];
		StringCchPrintf(strFile, RTL_NUMBER_OF(strFile),
			_T("D:\\Leen\\%02d.png"),i);
		SavePictureAsPng(strFile);
		Sleep(300);
	}

	LaunchWithShellExecuteEx(_T("D:\\Leen\\TestGif.exe"), NULL, SW_SHOW,TRUE);
	LaunchWithShellExecuteEx(_T("D:\\Leen\\Test.Gif"), NULL, SW_SHOW, FALSE);

	// Main message loop:
	/*while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}*/

	return (int) msg.wParam;
}


png合成GIF,这个地方使用了一个哥们用C#写的dll,所以要使用CLR

int main(array<System::String ^> ^args)
{
  //  Console::WriteLine(L"Hello World");
	String^ outputFilePath = L"D:\\Leen\\test.gif";
	AnimatedGifEncoder^ e = gcnew AnimatedGifEncoder;
	e->Start(outputFilePath);
	e->SetDelay(200);
	//-1:no repeat,0:always repeat
	e->SetRepeat(0);

	int i = 0;
	for ( i = 0; i < 10; i++)
	{
		String^ imageFilePath = String::Format(L"D:\\Leen\\{0:00}.png", i);
		e->AddFrame(Image::FromFile(imageFilePath));
	}
	

	e->Finish();
	/* extract Gif */
	String^ outputPath = L"D:\\Leen\\";
	GifDecoder^ gifDecoder = gcnew GifDecoder();
	gifDecoder->Read(outputFilePath);
	for (int i = 0, count = gifDecoder->GetFrameCount(); i < count; i++)
	{
		Image^ frame = gifDecoder->GetFrame(i);  // frame i
		frame->Save(outputPath + Guid::NewGuid().ToString() + L".png", ImageFormat::Png);
	}


    return 0;
}
</pre><pre class="cpp" name="code" snippet_file_name="blog_20160510_5_5738037" code_snippet_id="1677473">


技术分享

参考资料:

技术分享



技术分享


合成GIF的DLL

C++调用C#Dll

截取屏幕生成GIF

标签:

原文地址:http://blog.csdn.net/linlin003/article/details/51362370

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!