Using GDI + for image processing

Source: Internet
Author: User
Tags bool cos sin

Someone asked me how to rotate the image processing, that is, let the user crooked neck to see the image, the user's neck screwed up how to do? Won't come to me anyway ...

In fact, a solution to this problem is to use two-dimensional (x,y coordinate) matrix transformation to achieve image rotation. It can be solved by using trigonometry knowledge from high school or linear algebra in university. The principle is to know the coordinates of a point, then the rotation coordinates of this point can be (x*cos(A) + y*sin(A),- x*sin(A) + y*cos(A)) obtained, where a is the angle in radians (2P radians = 360 degrees). Therefore, as long as the image is loaded into memory, then it is selected into the device context, then call GetPixel and SetPixel, as described above, map all the pixels, you can achieve the rotation effect of the image. For 90, 180,-90 degrees of rotation, this is a good method, because the sine, cosine of the value of no more than +/-1 or 0 ... Before I finished, a brick flew over my head, ah yo, of course I will not ask you to do things in this way! There's a better way. Here is the point of this article: use GDI + to achieve image rotation processing. I want to understand GDI + is not a lot of people, because it is a new thing in Windows. For details about this, refer to the MSDN articles.

GDI + is an enhanced version of the GDI graphics library, which can be used by C + +. It is built in Windows XP and Microsoft. NET, and for Windows 98, Windows NT, and Windows 2000, there is a republishing version. GDI + is a C + + API. It uses C + + classes and C + + methods. GDI + contains a lot of content, much more than what I've described here. To use GDI +, you must include (#include) <gdiplus.h> files and link the project to the Gdiplus.lib library, which is included in the latest Windows SDK. two files. I have rewritten the example code Myimgapp in the article "displaying jpg/gif images in MFC programs" and renamed it Myimgapp2, whose code has made significant changes to the CPicture class, since the original cpicture is mainly encapsulated for IPicture, This time, the main encapsulation is GDI +. Here is the MYIMGAPP2 run screen, as shown in figure one. This program demonstrates how to rotate an image with GDI +.

Figure one image rotates 90 degrees

As mentioned earlier, the C + + class CPicture in the original program is based on the IPicture interface, which processes the COM interface of the image. In the example program Myimgapp2 of this article, I rewrote the code of CPicture, using the image class in GDI +. All the details are encapsulated. The new CPicture class no longer uses the IPicture COM interface, but replaces it with an image, and all other classes like CPictureView and Cpicturectrl work as before. But there are two hurdles to solve: the first is that you have to initialize the GDI library and terminate the release operation, so it is not so much an obstacle as it is the need of GDI + itself, where do these two operations? The best place to do this is in the InitInstance and ExitInstance functions of the program://初始化 GDI 
class CMyApp : public CWinApp {
protected:
 GdiplusStartupInput m_gdiplusStartupInput;
 ULONG_PTR m_gdiplusToken;
…….
};
//释放GDI
BOOL CMyApp::InitInstance()
{
 VERIFY(GdiplusStartup(&m_gdiplusToken,
  &m_gdiplusStartupInput, NULL)==Ok);
…….
}
int CMyApp::ExitInstance()
{
 GdiplusShutdown(m_gdiplusToken);
 return CWinApp::ExitInstance();
}
 

Cmyapp::m_gdiplustoken is a very magical thing, it comes from Gdiplusstartup and is passed to Gdiplusshutdown. M_gdiplusstartupinput is a structure that contains the startup parameters for some GDI +. The default constructor establishes an intelligent default value, which proves again that C + + is better than C. Once you start GDI +, you can use it. The original CPicture class has a pointer to the IPicture, and the new CPicture class has a pointer to image. Similarly, it also has a load function that can be overloaded to load the image from different places. For example, the following is the new version of CPicture How to load an image file from a path name.BOOL CPicture::Load(LPCTSTR pszPathName)
{
 Free();
 USES_CONVERSION;
 m_pImage = Image::FromFile(A2W(pszPathName),
  m_bUseEmbeddedColorManagement);
 return m_pImage->GetLastStatus()==Ok;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.