我将下载的字体保存在资源中,并希望在加载程序时将其设置在某个复选框上。
我已经尝试过了:
Checkbox1.Font = My.Resources.Allstar
(“Allstar”是字体的名称)
但它给了我一个错误:
类型'字节1维数组'的值无法转换为'System.Drawing.Font)
答案 0 :(得分:3)
将资源加载到PrivateFontCollection对象中。这样的事情应该很接近:
Dim lstPrivateFontCollection As New System.Drawing.Text.PrivateFontCollection
Dim objPointer As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(My.Resources.Allstar.length)
System.Runtime.InteropServices.Marshal.Copy(My.Resources.Allstar, 0, objPointer, My.Resources.Allstar.Length)
lstPrivateFontCollection.AddMemoryFont(objPointer, My.Resources.Allstar.Length)
'Omit next line due to instability pointed out in comments by Hans Passant.
'It was used to free memory, but could cause app to crash.
'System.Runtime.InteropServices.Marshal.FreeCoTaskMem(objPointer)
Dim objFont As New System.Drawing.Font(lstPrivateFontCollection.Families(0), 16.0F, System.Drawing.FontStyle.Regular, GraphicsUnit.Point)
lstPrivateFontCollection.Dispose()
Checkbox1.Font = objFont