Skip to content

Instantly share code, notes, and snippets.

@retorillo
Last active August 4, 2017 05:39
Show Gist options
  • Save retorillo/6b3c5387191c89d3d35f093b11fd2a01 to your computer and use it in GitHub Desktop.
Save retorillo/6b3c5387191c89d3d35f093b11fd2a01 to your computer and use it in GitHub Desktop.

GDI+

Gdiplus Namespace

GDI+ identifiers are defined inside Gdiplus namespace.

If compiler said "undefined identifier" error message, Check identifier's namespace, or consider to use using directive.

GdiplusStartup and GdiplusShutdown

Must call Gdiplus::GdiplusStartup before using GDI+. Otherwise, GDI+ methods will draw nothing. Also, should call Gdiplus::GdiplusShutdown on exit.

Gdiplus::GdiplusStartupInput gdiplusInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusInput, NULL);
...
Gdiplus::GdiplusShutdown(gdiplusToken);

https://msdn.microsoft.com/en-us/library/windows/desktop/ms534077(v=vs.85).aspx

Angle Unit (IN DEGREE!)

Generally GDI uses radian, but GDI+ uses degree as follows:

Matrix (3x3)

https://msdn.microsoft.com/en-us/library/ms534475(v=vs.85).aspx

A Matrix object stores only 6 of 9 numbers in 3 x 3 matrix. Because all 3 x 3 matrices that represent affine transformations have the same third column |0, 0, 1|

Matrix::Matrix(REAL, REAL, REAL, REAL, REAL, REAL)

Brushes

Brush base class
  |- SolidBrush
  |- HatchBrush
  |- LinearGradientBrush
  |- PathGradientBrush
  `- TextureBrush

Pen

GdiplusBase base class
  `- Pen class

Pen

Note that Pen can be created from Brush.

Pen::Pen(Color&, REAL)
Pen::Pen(const Brush*, REAL)

Path

https://msdn.microsoft.com/en-us/library/ms534456(v=vs.85).aspx

GraphicsPath => Graphics::DrawPath

Font

https://msdn.microsoft.com/en-us/library/ms534438(v=vs.85).aspx

FontCollection base class
  |- InstalledFontCollection
  `- PrivateFontCollection

GraphicContainer

Graphics containers are used to retain graphics state, such as transformations, clipping regions, and various rendering properties.

Note that, new container will reset compositing quality, smoothing mode, and etc.

g.SetSmootingMode(Gdiplus::SmoothingMode::SmoothingModeHighQuality);
// Smoothing mode is activated.
g.BeginContainer(...);
  // Inside new container, smoothing mode is reset and deactivated.
  // Draws shapes without antialiasing.
g.EndContainer();
// Smoothing mode is restored and actived. (maybe, not tested)

Shapes

Image & Bitmap & Metafile

Image class provides methods for loading and saving raster images and vector images: BMP, ICON, GIF, JPEG, Exif, PNG, TIFF, WMF, and EMF.

Image base class
  |- Bitmap
  `- Metafile

GdiplusBase
  `- CachedBitmap (NOTE: Not inherited class from Image)

Drawing

  • Graphics::DrawImage

Caching

  • Graphics::DrawCachedBitmap
  • Graphics::CachedBitmap

Metafile (*.EMF: Sequence of graphics API calls)

The Metafile class defines a graphic metafile. A metafile contains records that describe a sequence of graphics API calls. Metafiles can be recorded (constructed) and played back (displayed).

Metafile is stored as EMF Metafile: Enhanced Metafile

BOOL CALLBACK metaCallback(
   EmfPlusRecordType recordType, 
   unsigned int flags, 
   unsigned int dataSize, 
   const unsigned char* pStr, 
   void* callbackData)
{ 
   // Play only EmfPlusRecordTypeFillEllipse records.
   if (recordType == EmfPlusRecordTypeFillEllipse)
   {
   // Explicitly cast callbackData as a metafile pointer, and use it to call
   // the PlayRecord method.
   static_cast < Metafile* > (callbackData)->PlayRecord(recordType, flags, dataSize, pStr);
   }
   return TRUE; 
}

VOID Example_EnumerateMetafile(HDC hdc)
{
   Graphics graphics(hdc);
   // Create a Metafile object from an existing disk metafile.
   Metafile* pMeta = new Metafile(L"SampleMetafile.emf", hdc);
   {
      // Fill a rectangle and an ellipse in pMeta.
      Graphics metaGraphics(pMeta);
      SolidBrush blackBrush(Color(255, 0, 0, 0));
      SolidBrush redBrush(Color(255, 255, 0, 0));
      metaGraphics.FillRectangle(&blackBrush 0, 0, 100, 100);
  metaGraphics.FillEllipse(&redBrush 100, 0, 200, 100);
   }
   // Enumerate pMeta, passing pMeta as the callback data. 
   graphics.EnumerateMetafile(pMeta, Point(0, 0), metaCallback, pMeta);
   graphics.DrawImage(pMeta, Point(0, 150));
}

Text

Drawing

Mesurement

Antialiasing

Bitmap Effects

https://msdn.microsoft.com/en-us/library/ms534433(v=vs.85).aspx

Effect base class => Bitmap::ApplyEffect, Graphics::DrawImage
 |- Blur
 |- Sharpen
 |- Tint
 |- RedEyeCorrection
 |- ColorMatrixEffect
 |- ColorLUT
 |- BrightnessContrast
 |- HueSaturationLightness
 |- ColorBalance
 |- Levels
 `- ColorCurve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment