How is the default constructor for System.Drawing.Graphics removed?

When I try to create a Graphics object, why doesn't it work?

System.Drawing.Graphics graphicsObj = new System.Drawing.Graphics(); 

(I know that I could create a private System.Windows.Forms.Panel Obj; and then do CreateGraphics() if I wanted it to work)

I tried to find a custom constructor for Graphics, but I could not find it. Where did Microsoft identify it or how was it blocked?

+6
constructor c # graphics default-constructor
source share
3 answers

Default constructors are only created by the C # compiler if there are no other declared constructors. In this case, everything looks like internal or private. Basically you do not build yourself - you request one of the image, control or something else, or get it for drawing events, etc.

+10
source share

Intuitively, Graphics cannot have a default constructor. You always want you to draw in order to be visible somewhere. The default constructor could not select the drawing destination.

Ways to get a Graphics object:

  • Graphics.FromImage() . You are drawing a bitmap. Normal when resizing images or creating a canvas.
  • Control.CreateGraphics() . Let you draw directly on the screen. Always wrong, use: instead
  • Paint event. The e.Graphics argument allows you to draw on the screen.
  • PrintPage event. For the PrintDocument class, e.Graphics ends on a piece of paper.
  • Graphics.FromHdc() . Use P / Invoke in low-level code when referring to a Windows device context.
  • Graphics.FromHwnd() . As above, draws directly into its own window.

In short:

  • Draw a screen with the Paint event
  • Draw a printer using the PrintPage event
  • Draw a bitmap using FromImage()
+3
source share

Microsoft did not provide Graphics object constructors. The only way to create an instance is with static methods like CreateGraphics () or FromImage (). That is why your code is not working. Also, as a side element, a Graphics object cannot be inherited.

0
source share

All Articles