How to manually get instance of Graphics object in WinForms?

前端 未结 5 1110
萌比男神i
萌比男神i 2020-12-19 01:50

I know how to work with object of type Graphics (at least I am able to render images) but I always do that by passing graphics object retrieved from OnPaint method.

5条回答
  •  情书的邮戳
    2020-12-19 02:40

    None of the preceding answers worked for me. I found Rajnikant Rajwadi solution effective (see https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce90eb80-3faf-4266-b6e3-0082191793f7/creation-of-graphics-object-in-wpf-user-control?forum=wpf)

    Here is a horribly condensed call to Graphics.MeasureString(). (please code more responsibly)

    SizeF sf = System.Drawing.Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle).MeasureString("w", new Font(TheControl.FontFamily.ToString(), (float)TheControl.FontSize));
    

提交回复
热议问题