How can I embed font in Visual Basic .Net application? which should valid at every operating system.
It is possible to embed a font in an application and use it if that font is not available on the user's system.
You simply create a PrivateFontCollection and populate it with your fonts then you can use them as you please. According to MSDN, this method does not apply to operating systems before Windows 2000.
From Remarks section of PrivateFontCollection.AddFontFile method:
When using a private font on operating systems before Windows 2000, the default font, typically Microsoft Sans Serif, will be substituted.
If you intend your application to be used on Windows 2000 and newer, you can follow this code I wrote to see how to implement private fonts.
Public Class Form1
Dim pfc As System.Drawing.Text.PrivateFontCollection
Dim ifc As System.Drawing.Text.InstalledFontCollection
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
pfc = New System.Drawing.Text.PrivateFontCollection()
ifc = New System.Drawing.Text.InstalledFontCollection()
LoadPrivateFonts({My.Resources.Series_60_ZDigi, My.Resources.Times_NR_Phonetics_2})
End Sub
''' <summary>Loads the private fonts.</summary>
''' <param name="fonts">The fonts to be loaded into the private font collection.</param>
Private Sub LoadPrivateFonts(ByVal fonts As IEnumerable(Of Byte()))
For Each resFont In fonts
pfc.AddMemoryFont(Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(resFont, 0), resFont.Length)
Next
End Sub
''' <summary>Gets the FontFamily whose name matches the one specified.</summary>
''' <param name="fontName">Name of the FontFamily to be returned.</param>
''' <param name="defaultFamily">
''' Optional. The default font family to be returned if the specified font is not found
''' </param>
Private Function GetFontFamily(ByVal fontName As String, Optional ByVal defaultFamily As FontFamily = Nothing) As FontFamily
If String.IsNullOrEmpty(fontName) Then
Throw New ArgumentNullException("fontName", "The name of the font cannont be null.")
End If
Dim foundFonts = From font In ifc.Families.Union(pfc.Families) Where font.Name.ToLower() = fontName.ToLower()
If foundFonts.Any() Then
Return foundFonts.First()
Else
Return If(defaultFamily, FontFamily.GenericSansSerif)
End If
End Function
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
'free the resources used by the font collections
pfc.Dispose()
ifc.Dispose()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g = e.Graphics
Using br As Brush = Brushes.Black
g.DrawString("1234567890ABCDEF", New Font(GetFontFamily("Series 60 ZDigi"), 18), br, New Point(20, 20))
g.DrawString("ABCDEFGHIJKLMNOP", New Font(GetFontFamily("Times NR Phonetics 2"), 18), br, New Point(20, 100))
End Using
End Sub
End Class
I load the two fonts I use in the application (Series 60 ZDigi, a font from my Nokia phone, and Times NR Phonetics 2, a font from my dictionary application) from the resources into the private font collection in the Sub New().
I then call the GetFontFamily method to get the desired font to paint onto the form.
It shouldn't be too hard to incorporate this into your applications.
Cheers.
You can't. There is no cross-platform standard at this point for embedded fonts in a standard Winforms application. The best you can do is embed individual, OS-specific font files, detect which OS you're on, and then install the font programmatically.
On the other hand, A WPF application in VB.net might work for your needs, but I have a feeling this isn't where you're going. For information on how to package fonts with a WPF application, see this MSDN article.
Related
I have been trying (without success) to use and embedded font in my VB project. I have done it before but now I can't remember how to do it. I have been searching and searching on here and other websites, but stuff I have tried isn't working.
This is the only way I can do it at the moment. It works but I want to embed the font.
Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
privateFonts.AddFontFile("D:\somefont.ttf")
Dim font As New System.Drawing.Font(privateFonts.Families(0), 12)
Label1.Font = font
I have embedded the font no problem.
What code do I need to pull the font from the resource file and use it, on a label or panel, for example.
I know this has been asked so many times before, but nothing I try is working.
Thanks (with some embarrassment) in advance.
Neil.
I found a working solution on another site.
Imports System.Runtime.InteropServices
Public Class Form1
Public privateFontCollection As New Drawing.Text.PrivateFontCollection
Public FamilyName As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
Dim data As System.IntPtr = Marshal.AllocCoTaskMem(My.Resources.amstrad_cpc464.Length - 1)
Marshal.Copy(My.Resources.amstrad_cpc464, 0, data, My.Resources.amstrad_cpc464.Length)
privateFontCollection.AddMemoryFont(data, My.Resources.amstrad_cpc464.Length)
Marshal.FreeCoTaskMem(data)
Dim fnt As Font = New Font(privateFontCollection.Families(0), 15)
Label1.Font = fnt
End Sub
End Class
I want to display 🡸 (a WIDE-HEADED LEFTWARDS HEAVY BARB ARROW Symbol) in a PrintDocument. It does not render correctly; it only shows a box. Using Windows Forms with vb.net 4.6; Visual Studio 2017.
I know I have the correct character and font because it displays in a button just fine on the form. Yet in the PrintPreviewDialog it won't render correctly. What do I need to do?
Public WithEvents Doc As New PrintDocument
Public Sub ShowDialog()
'page settings
Me.Doc.DefaultPageSettings = ... set letter size, default printer, margins
Me.Doc.PrinterSettings = ...default printer
Me.Doc.DefaultPageSettings.PaperSize.RawKind = PaperKind.Letter
'show dialog
Dim dlgPreview As New PrintPreviewDialog
dlgPreview.Document = Me.Doc
dlgPreview.WindowState = FormWindowState.Maximized
dlgPreview.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles Doc.PrintPage
Dim arrow As String = Char.ConvertFromUtf32(&H1F87A)
Dim f As New System.Drawing.Font("Microsoft Sans Serif", 11, FontStyle.Regular)
e.Graphics.DrawString(arrow, f, Brushes.Black, 100, 100)
End Sub
By looping through all my installed fonts I was able to find a font that displays the correct character. In my case it was "Segoe UI Symbol".
I'm trying to use the VlcDotNet library to embed a small media player into a VB.Net forms-based project.
Mainly I'm following the solution that I found here:
Display video with VlcDotNet library
except that I've had to translate from C# to VB.
Having followed the instructions to the letter, there are two lines of the code which simply aren't recognised and won't compile. Certain methods of the Vlc.DotNet.Core code simply aren't accepted as valid.
Having used NuGet to get 4 Vlc.DotNet packages (.Core, .Core.Interops, .Forms and .Wpf), my complete code (excluding the very basic design of one button and one panel) is below. The compiler won't compile it, because it says there are 2 errors.
Firstly, "Type Vlc.DotNet.Core.Medias.MediaBase is not defined" (for the 'Dim newMedia as...' line)
Secondly, "'Media' is not a member of 'Vlc.DotNet.Forms.Control" (for the '.Media = newMedia' line)
Almost all the methods of the VlcControl are present, but I can't see why certain elements, such as the Media method, are simply not there?
Any suggestions would be extremely welcome.
Thanks
Imports Vlc.DotNet.Core
Imports Vlc.DotNet.Core.Interops
Imports Vlc.DotNet.Forms
Imports Vlc.DotNet.Wpf
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim player As New Vlc.DotNet.Forms.VlcControl
Panel1.Controls.Add(player)
With player
.BackColor = Color.Blue
.ImeMode = Windows.Forms.ImeMode.NoControl
.Location = New System.Drawing.Point(0, 0)
.Name = "test"
.Rate = 0.0F
.Size = Panel1.Size
Dim newMedia As Vlc.DotNet.Core.Medias.MediaBase = New Vlc.DotNet.Core.Medias.PathMedia("E:\TempTest.mov")
.Media = newMedia
.Play()
End With
End Sub
End Class
I had try to convert out from C# and below are the new library files using method:
Public myvlc As New VlcControl
myvlc.Dock = DockStyle.None
myvlc.Size = New Size(myscreen.Bounds.Width, myscreen.Bounds.Height)
myvlc.Location = New Point(0, 0)
Me.Controls.Add(myvlc)
AddHandler myvlc.VlcLibDirectoryNeeded, AddressOf LibNeeded
Private Sub LibNeeded(ByVal Sender As Object, ByVal e As VlcLibDirectoryNeededEventArgs)
e.VlcLibDirectory = New IO.DirectoryInfo("C:\lib\x86\")
End Sub
Please download the Video Codec lib from the same website where you get the vlc.dotnet.
I'm having trouble creating a .gif be animated in my already created visual studio 2013 project.
Been looking online a bit for the "proper" way to do it. Following this MSDN question: Link I copied some of the code to see how it would work. I've also seen references to using a timer.
Public Not Inheritable Class SplashScreen
Private progressGifPath As String = "C:\Documents\Visual Studio 2013\Projects\CameraFinder\icons" + "\orangeLoading.gif"
Private _AnimatedGif As New Bitmap(progressGifPath)
Private m_IsAnimating As Boolean
Public Property IsAnimating() As Boolean
Get
Return m_IsAnimating
End Get
Set(ByVal value As Boolean)
m_IsAnimating = value
End Set
End Property
Private Sub VICameraFinderLoading_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set up the dialog text at runtime according to the application's assembly information.
If My.Application.Info.Title <> "" Then
Else
End If
End Sub
Private Sub PlayGIF()
If Not m_IsAnimating Then
ImageAnimator.Animate(_AnimatedGif, New EventHandler(AddressOf Me.OnFrameChanged))
m_IsAnimating = True
End If
End Sub
Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
If m_IsAnimating Then
ImageAnimator.UpdateFrames()
Dim aGraphics As Graphics = PictureBox.CreateGraphics
aGraphics.DrawImage(_AnimatedGif, New Point(30, 30))
aGraphics.Dispose()
End If
End Sub
End Class
So I tried this, but I don't see anything on the picture box drawn (I confirmed to see it was brought to the front). And apparently there can be a size limit to the gif included. Any ideas on what would be the better way to include it?
Thanks!
Figured it out. I hadn't set the image of the picture box to the gif.
EDIT:
To clarify, the code above wasn't the problem at all. I actually didn't need it in any way. I removed it from the splash screen class, completely. The solution was to go into my picture box's property (F4) and set the backgroundImage to the .gif's filepath (thus importing it as an embedded resource). Doing so has allowed for animation in the splash screen.
I want to render a PDF page in a control in winforms and then move rectangles around over the PDF to identify user selected text strings. I'm trying to render the PDF using a WebBrowser control but WebBrowser doesn't seem to support GDI.
Can anyone suggest a better way of rendering the PDF so that I can move rectangles around on it.
If you want to continue using the WebBrowser Control you can use a transparent form that moves and resizes with the underlying form.
Create your mainform Form1 and add a Webbrowsercontrol to it. For this example set .Dock to All.
Add a second form, Form2 with nothing on it.
In Form1 you show Form2 and move it if the Form moves or resizes.
Public Class Form1
Private Sub MoveForm2()
Dim crpos As Point = Me.PointToClient(Me.DesktopLocation)
With Form2
.DesktopLocation = New Point(Me.DesktopLocation.X - crpos.X, Me.DesktopLocation.Y - crpos.Y)
.WindowState = Me.WindowState
.Size = Me.ClientSize
End With
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("www.google.com")
MoveForm2()
Form2.Show(Me)
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
MoveForm2()
End Sub
Private Sub Form1_Move(sender As Object, e As EventArgs) Handles MyBase.Move
MoveForm2()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
MoveForm2()
End Sub
End Class
In Form2 you use an API call to let you click through Form2 (ripped from VB.net Click through form ).
Here you also draw directly onto the form. Use TransparencyKey and BackColor to make it transparent.
Imports System.Runtime.InteropServices
Public Class Form2
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong")> Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function
Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
'Draw rectangles here
Using g As Graphics = Me.CreateGraphics
g.DrawRectangle(Pens.Red, 100, 100, 100, 100)
End Using
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Pink
Me.TransparencyKey = Color.Pink
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Dim InitialStyle As Integer
InitialStyle = GetWindowLong(Me.Handle, -20)
SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20) 'Makes the window "click-throughable"
End Sub
End Class
This is a rather dirty hack of course but if you just want to move the rectangles yourself it should work quite well. You have to adapt this example to your needs of course.
This solution may work in the following 2 scenarios:
1- If you are generating the PDF file yourself and you are willing to switch over to PDFSharp for generating the file.
2- If you are not generating input files but you are ok with displaying a modified version that contains the rectangles that you want to be displayed.
I use PDFsharp. Is a open source .NET library for processing PDF
http://www.pdfsharp.com/PDFsharp/
Added:
Graphics
The graphical objects follow the design pattern of the .Net framework. With one set of functions you can draw on a PDF page as well as on a System.Drawing.Graphics object. Your application can render its output in a window, on the printer or in a PDF document.
Lines, polylines, arcs, Bézier splines, canonical splines
Rectangles, rounded rectangles, ellipses, polygons, pies, closed splines, paths
PDFsharp is the Open Source library that easily creates PDF documents from any .NET language.
The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.
PDFsharp Highlights
Creates PDF documents on the fly from any .Net language
Easy to understand object model to compose documents
One source code for drawing on a PDF page as well as in a window or on the printer
Modify, merge, and split existing PDF files
Images with transparency (color mask, monochrome mask, alpha mask)
Newly designed from scratch and written entirely in C#
PDFsharp Features
Key Features
Creates PDF documents on the fly from any .Net language
Easy to understand object model to compose documents
One source code for drawing on a PDF page as well as in a window or on the printer
Modify, merge, and split existing PDF files
Images with transparency (color mask, monochrome mask, alpha mask)
Newly designed from scratch and written entirely in C#
The graphical classes go well with .Net
Can use either GDI+ or WPF