当前位置:网站首页>Basic image transformation in gdi+

Basic image transformation in gdi+

2022-06-09 12:45:00 litanyuan

background

Graph transformation refers to the translation of the drawn graph 、 rotate 、 Telescoping and other operations , from Graphics Class provides corresponding member functions for implementation

Translation transformation

①. summary
Translation transform will draw the coordinates of the drawing (x, y) Translate all by one increment (dx, dy), Corresponding member function TranslateTransform , It is equivalent to moving the origin of the coordinate system of the drawing .

②. Translation transformation

void DemoGDI::DrawUser(HDC hdc)
{
    
  Graphics graphics(hdc);// structure  Graphics  object 
  Pen m_pen(Color::Blue, 2);
  Rect m_rect(30, 30, 180, 120);

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 

  graphics.TranslateTransform(20, 40);//x  translation 20,y  translation 40

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 

}

 Insert picture description here

Rotation transformation

①. summary

Rotate the coordinates of the drawing (x, y) Rotate all α degree , Is the rotation relative to the coordinate origin of the upper left corner . Corresponding member function RotateTransform.

②. Rotation transformation

void DemoGDI::DrawUser(HDC hdc)
{
    
  Graphics graphics(hdc);// structure  Graphics  object 
  Pen m_pen(Color::Blue, 2);
  Rect m_rect(30, 30, 180, 120);

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 

  graphics.RotateTransform(10);// rotate 10 degree 

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 

}

 Insert picture description here

Scaling transformation

①. Concept
Stretch transform will draw the shape of the figure in x and y In the direction of the specified scale sx and sy Zoom , Corresponding member function ScaleTransform, The transformation is the value of each coordinate point in the graph .

②. Scaling transformation

void DemoGDI::DrawUser(HDC hdc)
{
    
  Graphics graphics(hdc);// structure  Graphics  object 
  Pen m_pen(Color::Blue, 2);
  Rect m_rect(30, 30, 180, 120);

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 
  
  graphics.ScaleTransform(0.3f, 0.3f);//x、y  Change to the original  0.3

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 

}

 Insert picture description here

③. Mirror transformation

void DemoGDI::DrawUser(HDC hdc)
{
    
  Graphics graphics(hdc);// structure  Graphics  object 
  Pen m_pen(Color::Blue, 2);

  Font font(L" Song style ", 30, FontStyleRegular, UnitPixel);  
  SolidBrush solidBrush(Color::Blue);
  StringFormat stringFormat;
  stringFormat.SetLineAlignment(StringAlignmentCenter);

  graphics.TranslateTransform(250, 40);// Change coordinate origin 

  QString m_text = "GDI+ Text rendering ";
  graphics.DrawString(m_text.toStdWString().c_str() , -1, &font, PointF(0.0f, 40.0f), &stringFormat, &solidBrush);

  graphics.ScaleTransform(-1, 1);// Symmetry up and down 

  graphics.DrawString(m_text.toStdWString().c_str(), -1, &font, PointF(0.0f, 40.0f), &stringFormat, &solidBrush);

}

 Insert picture description here

Rectangle scaling

①. summary
Rect Class provides member functions Inflate Used for scaling rectangles , The scaling result is to extend or shrink the four edges of the rectangle by a specified distance .

②.Inflate Use

void DemoGDI::DrawUser(HDC hdc)
{
    
  Graphics graphics(hdc);// structure  Graphics  object 
  Pen m_pen(Color::Blue, 2);
  Rect m_rect(30, 30, 180, 120);

  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 

  m_rect.Inflate(-10, -10);//4 The edge shrinks inward 10 Pixel 
  
  graphics.DrawRectangle(&m_pen, m_rect);// Draw a rectangle 
  
}

 Insert picture description here

原网站

版权声明
本文为[litanyuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091152531873.html