GDI + image processing learning notes-change image transparency

Source: Internet
Author: User

GDI + is a sub-system in windows XP and windows Server 2003. It is mainly used to process system message rendering. GDI + is an extension of GDI. It inherits a bit of GDI and makes improvements based on it, including adding some graphic functions that cannot be drawn by GDI, at the same time, the GDI + re-designed programming model makes it easier to develop graphical programs. As a graphical device interface, GDI + allows application developers to output screen and printer information without considering the details of specific display devices, they only need to call some methods of the class output by the GDI + Library to complete graphic operations. The real drawing work is done by these methods to specific device drivers, GDI + isolates graphics hardware and applications. this makes it easy for developers to write device-independent applications.

In GDI +, in addition to the three components of red, green, and blue, a variable called Alpha is added to indicate the transparency of the color. The value range is 0-255. in this example, the change of the image transparency is mainly to change the Alpha value of each color in the image to achieve the effect of changing the transparency.

In this example, it is developed in VS2010. The specific effect is as follows:

 

Open the image file. The default transparency is 0 and the corresponding alpha value is 1. Click the color processing menu to open the image transparency dialog box. After changing the value of the sliding block, the image transparency changes accordingly.



 

The following describes the implementation process in detail:


Configuration of GDI + in vs2010
According to my actual test, in VS2010, you only need to go to the resource file stdafx. h is included in Gdiplus. the h header file is ready, and you do not need to add the GDI + library file in the project properties, in the document "Digital Image Processing-Principles and Practices" written by Zuo Fei, under VS2005, it seems that you want to add it to project properties-> Configuration properties-> linker-> input-> additional dependencies. gdiplus. lib. My machine does not have VS2005, So I dare not draw conclusions. However, in VS2010, you only need to add the following code in stdafx. h.
[Cpp] # include <GdiPlus. h>
Using namespace Gdiplus;
# Include <GdiPlus. h>
Using namespace Gdiplus; 2. Changing the principle of image transparency
As mentioned before, an alpha variable is added to GDI + to indicate the transparency of the image, however, the alpha value in the image cannot be directly changed in the GDI + (I am also a beginner, it is not long before, this sentence is not easy to say for the moment ), therefore, we need to use a struct called ColorMatrix, which is a 5*5 array. We use it to represent the color transformation relationship. The following is the definition of ColorMatrix in GDI +.
[Cpp] typedef struct {
REAL m [5] [5];
} ColorMatrix;
Typedef struct {
REAL m [5] [5];
} ColorMatrix; I did not find a specific definition of the REAL type in Microsoft's MSDN. in the book "Digital Image Processing-Principles and Practices", it represents a single-precision floating point type.
The ColorMatrix mentioned above is described below as a color transformation formula under GDI +.


1 0 0 0 0
0 1 0 0 0

[R g B a 1] = [r g B a 1] 0 0 1 0

0 0 0 1 0

0 0 0 0 1

R/G/B/A indicates the red, green, blue, and transparency components after the color transformation, r/g/B/a indicates the red, green, blue, and transparency components of the image before the color transformation, followed by a 5*5 matrix, this matrix is the 5*5 array of the ColorMatrix struct. According to the multiplication of the matrix, to change the transparency of the image, you only need to define a ColorMatrix structure and change the number of the fourth column in the fourth row to the corresponding transparency.

3. Implementation of the image transparency dialog box

The dialog box is very simple. Add two widgets: an edit box, bind the variable m_alpha to it, the type is REAL, and the other is the sliding block control. Then, load the message WM_HSCROLL in the main dialog box, after reloading, an OnHScroll message response function is automatically generated in the dialog box class. Our main operation is to implement this function class. below is the code of this function class.

 

[Cpp] BOOL CImageDlg: OnInitDialog ()
{
CDialogEx: OnInitDialog ();
CSliderCtrl * pSlider;
PSlider = (CSliderCtrl *) GetDlgItem (IDC_SLIDER_ALPHA); // bind the control according to the Control ID
PSlider-> SetRange (0,100); // you can specify the range of the slider.
// TODO: add additional initialization here
 
Return TRUE; // return TRUE unless you set the focus to a control
// Exception: the OCX attribute page should return FALSE.
}
BOOL CImageDlg: OnInitDialog ()
{
CDialogEx: OnInitDialog ();
CSliderCtrl * pSlider;
PSlider = (CSliderCtrl *) GetDlgItem (IDC_SLIDER_ALPHA); // bind the control according to the Control ID
PSlider-> SetRange (0,100); // you can specify the range of the slider.
// TODO: add additional initialization here

Return TRUE; // return TRUE unless you set the focus to a control
// Exception: the OCX attribute page should return FALSE.
}
[Cpp] void CImageDlg: OnHScroll (UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
 
{
// TODO: add the message processing program code and/or call the default value ............................. .............
CSliderCtrl * pSlider = (CSliderCtrl *) pScrollBar;
M_alpha = (100.0-pSlider-> GetPos ()/100.0;
UpdateData (FALSE );
Void CImageDlg: OnHScroll (UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)

{
// TODO: add the message processing program code and/or call the default value ............................. .............
CSliderCtrl * pSlider = (CSliderCtrl *) pScrollBar;
M_alpha = (100.0-pSlider-> GetPos ()/100.0;
UpdateData (FALSE); [cpp] // obtain the pointer of the View class and refresh the View
// Obtain the pointer of the View class and refresh the View [cpp] CMainFrame * pMainFrame = (CMainFrame *) AfxGetMainWnd ();
CGdiPlus_ColorView * pView = (CGdiPlus_ColorView *) pMainFrame-> GetActiveView ();
// PView-> UpdateWindow ();
PView-> Invalidate ();
// UpdateWindow ();
CDialogEx: OnHScroll (nSBCode, nPos, pScrollBar );
}
CMainFrame * pMainFrame = (CMainFrame *) AfxGetMainWnd ();
CGdiPlus_ColorView * pView = (CGdiPlus_ColorView *) pMainFrame-> GetActiveView ();
// PView-> UpdateWindow ();
PView-> Invalidate ();
// UpdateWindow ();
CDialogEx: OnHScroll (nSBCode, nPos, pScrollBar );
} 4. related operations in the Doc document class
Add the variable image to save the opened image file, and reload the OnOpenDocument function in the document class.
[Cpp] public:
Virtual BOOL OnOpenDocument (LPCTSTR lpszPathName );
Image * image;
Public:
Virtual BOOL OnOpenDocument (LPCTSTR lpszPathName );
Image * image; saves the opened Image file to the Image variable in the OnOpenDocument function.
[Cpp] BOOL CGdiPlus_ColorDoc: OnOpenDocument (LPCTSTR lpszPathName)
{
If (! CDocument: OnOpenDocument (lpszPathName ))
Return FALSE;
// TODO: add your dedicated creation code here
Image = Image: FromFile (lpszPathName );
// UpdateAllViews (NULL );
Return TRUE;
}
BOOL CGdiPlus_ColorDoc: OnOpenDocument (LPCTSTR lpszPathName)
{
If (! CDocument: OnOpenDocument (lpszPathName ))
Return FALSE;
// TODO: add your dedicated creation code here
Image = Image: FromFile (lpszPathName );
// UpdateAllViews (NULL );
Return TRUE;
}


5. related operations in the View class

First, you must add two variables, one of which is the alpha value, and the other is the pointer to the image transparency dialog box;

[Cpp] public:
REAL alpha; // image transparency
CImageDlg * dlg; // image transparency dialog box pointer
Public:
REAL alpha; // image transparency
CImageDlg * dlg; // image transparency dialog box pointer

Add the image processing menu item to the menu bar in the resource file, bind the time processing program to it, and display the image transparency dialog box in non-modal mode in the time processing program.

[Cpp] void CGdiPlus_ColorView: OnImageAlpha ()
{
// TODO: add the command handler code here
Dlg-> Create (IDD_DIALOG1, this );
Dlg-> ShowWindow (SW_SHOW); // non-Modal Dialog Box
}
Void CGdiPlus_ColorView: OnImageAlpha ()
{
// TODO: add the command handler code here
Dlg-> Create (IDD_DIALOG1, this );
Dlg-> ShowWindow (SW_SHOW); // non-Modal Dialog Box
} Finally, plot in the OnDraw function.

[Cpp] void CGdiPlus_ColorView: OnDraw (CDC * pDC)
{
CGdiPlus_ColorDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
If (! PDoc)
Return;
If (pDoc-> image = NULL)
{
Return;
}
Alpha = dlg-> m_alpha;
Void CGdiPlus_ColorView: OnDraw (CDC * pDC)
{
CGdiPlus_ColorDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
If (! PDoc)
Return;
If (pDoc-> image = NULL)
{
Return;
}
Alpha = dlg-> m_alpha; [cpp] // obtain the image width and height
Int nWidth = pDoc-> image-> GetWidth ();
Int nHight = pDoc-> image-> GetHeight ();
// Declare a blank image
Bitmap bitmap (nWidth, nHight );
Rect rect (0, 0, nWidth, nHight );
 
Graphics temp (& bitmap); // draw a new image

ColorMatrix colorMartrix = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, alpha, 0,
0, 0, 0, 1 };
ImageAttributes imageAttr;
ImageAttr. SetColorMatrix (& colorMartrix );
Temp. DrawImage (pDoc-> image, rect, 0, 0, nWidth, nHight, UnitPixel, & imageAttr );
 
// Create a memory environment compatible with the device
CDC memDC;
CBitmap MemBitmap;
CRect ClientRect;
GetClientRect (ClientRect );
MemDC. CreateCompatibleDC (NULL );
MemBitmap. CreateCompatibleBitmap (pDC, ClientRect. Width (), ClientRect. Height ());
MemDC. SelectObject (MemBitmap );
MemDC. FillSolidRect (255,255,255, ClientRect. Width (), ClientRect. Height (), RGB ));
Graphics graph (memDC. GetSafeHdc ());
// Graphics graph (pDC-> GetSafeHdc (); // obtain the handle of the Current Device
// Graph. DrawImage (pDoc-> image, 0, 0 );
Graph. DrawImage (& bitmap, 0, 0 );
PDC-> BitBlt (, nWidth, nHight, & memDC, SRCCOPY );
 
// TODO: add the drawing code for the local data here
// Obtain the image width and height
Int nWidth = pDoc-> image-> GetWidth ();
Int nHight = pDoc-> image-> GetHeight ();
// Declare a blank image
Bitmap bitmap (nWidth, nHight );
Rect rect (0, 0, nWidth, nHight );

Graphics temp (& bitmap); // draw a new image

ColorMatrix colorMartrix = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, alpha, 0,
0, 0, 0, 1 };
ImageAttributes imageAttr;
ImageAttr. SetColorMatrix (& colorMartrix );
Temp. DrawImage (pDoc-> image, rect, 0, 0, nWidth, nHight, UnitPixel, & imageAttr );

// Create a memory environment compatible with the device
CDC memDC;
CBitmap MemBitmap;
CRect ClientRect;
GetClientRect (ClientRect );
MemDC. CreateCompatibleDC (NULL );
MemBitmap. CreateCompatibleBitmap (pDC, ClientRect. Width (), ClientRect. Height ());
MemDC. SelectObject (MemBitmap );
MemDC. FillSolidRect (255,255,255, ClientRect. Width (), ClientRect. Height (), RGB ));
Graphics graph (memDC. GetSafeHdc ());
// Graphics graph (pDC-> GetSafeHdc (); // obtain the handle of the Current Device
// Graph. DrawImage (pDoc-> image, 0, 0 );
Graph. DrawImage (& bitmap, 0, 0 );
PDC-> BitBlt (, nWidth, nHight, & memDC, SRCCOPY );
 
// TODO: add the drawing code for the local data here
} Two plotting methods are used. One is to output an image directly to the context of the current device, and the comments section in the code. The other is to first create a memory environment compatible with the device, and draw the image first in this memory environment and then copy it to the screen, this method can be used to remove the flickering screen that the view brings during the refreshing process. However, when I keep moving the slider, the view refresh is still very blinking. I have added the WM_ERASEBKGN message in the View class, as shown below, but it still cannot solve the problem.


[Cpp] BOOL CGdiPlus_ColorView: OnEraseBkgnd (CDC * pDC)
{
// TODO: add the message processing program code and/or call the default value here
// Return TRUE;
Return CView: OnEraseBkgnd (pDC );
}

From txg703003659
 

Related Article

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.