How can I convert a graphics object to image?

H

HKSHK

Hello,

I've got this problem:

I loaded an icon (with alpha and smoothing) into a graphics object.
Now I want to display it. The best solution would be if I could convert
it
into an image object so the picture box can display it.

Does anybody have an idea how I could do that?

Thanks a lot in advance.

Best Regards,

HKSHK

This is my source code:

'START
Private Declare Function ExtractIcon Lib "shell32.dll" Alias
"ExtractIconA" (ByVal hInst As IntPtr, ByVal lpszExeFileName As String,
ByVal nIconIndex As Int32) As IntPtr

Private sub Test (ByVal ExeOrDLL As String, ByVal ResourceID As Int32)

hIcon = ExtractIcon(IntPtr.Zero, ExeOrDLL, ResourceID)

ico = Icon.FromHandle(hIcon)

Dim g As Graphics = Me.PictureBox1.CreateGraphics
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.DrawIcon(ico, 0, 0)

'END
When calling it as Test ("C:\windows\system\user32.dll", 3) the
function will show the "Critical" icon (like MsgBox).
 
H

Herfried K. Wagner [MVP]

HKSHK said:
When calling it as Test ("C:\windows\system\user32.dll", 3) the
function will show the "Critical" icon (like MsgBox).

In addition to the reply I have given in the German VB.NET group, you may
want to check out 'SystemIcons' which provides easy access to message box
icons.
 
H

HKSHK

I found a solution, which doesn't convert the icon into bitmap without
loss, but at least updates it if necessary (when it was covered
before).

Const IconX1 As Byte = 12
Const IconY1 As Byte = 12
Private IconX2 As Byte = 0, IconY2 As Byte = 0
Private x As Byte = 0, y As Byte = 0
Private g As Graphics = Me.CreateGraphics
Private Ico As Icon
Private UpdateIcon As Boolean = False
Private g As Graphics = Me.CreateGraphics

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

Ico = SystemIcons.Error
IconX2 = IconX1 + Ico.Width
IconY2 = IconY1 + Ico.Height
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
UpdateIcon = False
For x = IconX1 To IconX2

For y = IconY1 To IconY2

If e.ClipRectangle.Contains(x, y) = True Then
g.DrawIcon(Ico, 0, 0)
UpdateIcon = True
Exit For
End If
Next y

If UpdateIcon = True Then
Exit For
End If
Next x
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top