VB.NET
<System.Runtime.InteropServices.DllImport( _
"user32.dll", _
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function
Private Sub btn1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles btn1.Click
Dim iconFile As String = "C:\test\1.ico"
Dim bmp As New Bitmap(32, 32)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillEllipse(Brushes.Red, 1, 1, 30, 30)
g.Dispose()
Dim ico As Icon = System.Drawing.Icon.FromHandle(bmp.GetHicon())
Dim fs As New System.IO.FileStream(iconFile, _
System.IO.FileMode.Create, System.IO.FileAccess.Write)
ico.Save(fs)
fs.Close()
ico.Dispose()
bmp.Dispose()
DestroyIcon(ico.Handle)
End Sub
C#[System.Runtime.InteropServices.DllImport(
"user32.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
private void btn1_Click(object sender, EventArgs e)
{
string iconFile = "C:\\test\\1.ico";
Bitmap bmp = new Bitmap(32, 32);
Graphics g = Graphics.FromImage(bmp);
g.FillEllipse(Brushes.Red, 1, 1, 30, 30);
g.Dispose();
Icon ico = System.Drawing.Icon.FromHandle(bmp.GetHicon());
System.IO.FileStream fs = new System.IO.FileStream(
iconFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
ico.Save(fs);
fs.Close();
ico.Dispose();
bmp.Dispose();
DestroyIcon(ico.Handle);
}