have added a TrueType font to my project resources ("MyFontResource"), and I've set the build action to "Resource." My intention is to replace the font on a Label object with this resource.
Here's my code:
PrivateFontCollection myFonts = new PrivateFontCollection();
unsafe {
fixed (byte* fontBytes = Properties.Resources.MyFontResource)
myFonts.AddMemoryFont((IntPtr)fontBytes, Properties.Resources.MyFontResource.Length);
}
myLabel.Font = new Font(myFonts.Families[0], 10f);
The font displays as expected only when I have the font installed locally. If I haven't installed the font, then I see the font originally assigned to myLabel in my C# project.
Now what?
Never mind, found the reason this doesn't work here.
Here's a solution that works (original code here):
class MyClass {
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
public MyClass() {
uint installCount = 1;
PrivateFontCollection myFonts = new PrivateFontCollection();
unsafe {
fixed (byte* pFontData = Properties.Resources.MyFont) {
myFonts.AddMemoryFont((IntPtr)pFontData, Properties.Resources.MyFont.Length);
AddFontMemResourceEx((IntPtr)pFontData, (uint)Properties.Resources.MyFont.Length, IntPtr.Zero, ref installCount);
}
}
myLabel.Font = new Font(myFonts.Families[0], 20f);
}
}
Related
i have got some problems with adding custom fonts, after creating a font object.
Here is some demo code:
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint wMsg, int wParam, int lParam);
[DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
public void Demo()
{
var font = new Font("Roboto", 8); //Create roboto font. Not installed yet, then use Times New Roman. Ok!
var result = AddFontResource(#"C:\Roboto.ttf"); //Install roboto font until system reboots (Sample filename). result == 1. Ok!
var test = SendMessage(0xffff, 0x001D, 0, 0);
font = new Font("Roboto", 8); //Font should be roboto, but still Times New Roman.
}
Then following demo works:
public void Demo()
{
var result = AddFontResource(#"C:\Roboto.ttf"); //Install roboto font until system reboots (Sample filename). result == 1. Ok!
var test = SendMessage(0xffff, 0x001D, 0, 0);
var font = new Font("Roboto", 8); //Font is roboto. OK!
}
Could somebody tell me why AddFontResourceW only works, if no font object was created earlier? Or how can i get the first example to run properly?
It's a while that I'm trying to figure out what's happening in this piece of code.
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont,
uint cbFont, IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
public static PrivateFontCollection myFontCollection = new PrivateFontCollection();
public static FontFamily RobotoBold = null;
public static FontFamily RobotoThin = null;
public enum Fonts
{
Roboto_Bold = 0,
Roboto_Thin = 1
}
public static void loadFonts()
{
Array fonts = Enum.GetValues(typeof(Fonts));
foreach (Fonts font in fonts)
{
byte[] fontData = (byte[])Resources.ResourceManager.GetObject(font.ToString());
IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
uint dummy = 0;
myFontCollection.AddMemoryFont(fontPtr, fontData.Length);
AddFontMemResourceEx(fontPtr, (uint)fontData.Length, IntPtr.Zero, ref dummy);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
}
RobotoBold = Program.myFontCollection.Families[(int)Program.Fonts.Roboto_Bold];
RobotoThin = Program.myFontCollection.Families[(int)Program.Fonts.Roboto_Thin];
}
Actually it reads the .ttf I've embedded as resources, but if I debug, inside the FontFamilys I read this message:
{Name = The name 'name' does not exist in the current context}
I've already tried to figure out what's happening, but can't find my error.
The resource is being read correctly using the ResourceManager, but I think something's wrong while adding the font.
I want to use SystemIcons.Warning but it is too big for my need. I want to resize it.
I have tried :
Icon sizedIcon = new Icon(SystemIcons.Warning, new Size(10,10));
But it does not work, icon remains the same.
Any suggestions?
The .NET Icon class is pretty crippled, it is stuck in the previous decade due to once-relevant support for Windows 98 and Windows 2000. On top of this, Windows does not support loading the system icons in any size other than the system's default icon size. Usually 32x32. Resizing it is going to inevitably look bad.
Do keep in mind that no icon is ever going to look good at 10x10. It is but a fleck on a modern monitor. You do get ahead a bit by starting with an icon size that's close to the desired final size, the less drastic the required resize, the higher the odds that it still looks reasonable. When you can, do favor sizes that are likely to be present in the icon. Like 16x16, 32x32, 48x48.
Anyhoo, this is fixable. Do keep in mind that this takes considerable hack-o-rama since Windows doesn't directly support this. What's required is reading the icon directly from the system DLL resources. And using a more modern winapi, LoadImage() instead of the one that .NET uses, LoadIcon(). Works on XP and up.
Add a new class to your project and paste this code:
using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
public class IconEx : IDisposable {
public enum SystemIcons {
Application = 100,
Asterisk = 104,
Error = 103,
Exclamation = 101,
Hand = 103,
Information = 104,
Question = 102,
Shield = 106,
Warning = 101,
WinLogo = 100
}
public IconEx(string path, Size size) {
IntPtr hIcon = LoadImage(IntPtr.Zero, path, IMAGE_ICON, size.Width, size.Height, LR_LOADFROMFILE);
if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
attach(hIcon);
}
public IconEx(SystemIcons sysicon, Size size) {
IntPtr hUser = GetModuleHandle("user32");
IntPtr hIcon = LoadImage(hUser, (IntPtr)sysicon, IMAGE_ICON, size.Width, size.Height, 0);
if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
attach(hIcon);
}
public Icon Icon {
get { return this.icon; }
}
public void Dispose() {
if (icon != null) icon.Dispose();
}
private Icon icon;
private void attach(IntPtr hIcon) {
// Invoke the private constructor so we can get the Icon object to own the handle
var ci = typeof(Icon).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(IntPtr), typeof(bool) }, null);
this.icon = (Icon)ci.Invoke(new object[] { hIcon, true});
}
private const int IMAGE_ICON = 1;
private const int LR_LOADFROMFILE = 0x10;
private const int LR_SHARED = 0x8000;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadImage(IntPtr hinst, string lpszName, int uType,
int cxDesired, int cyDesired, int fuLoad);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadImage(IntPtr hinst, IntPtr resId, int uType,
int cxDesired, int cyDesired, int fuLoad);
}
Sample usage:
using (var icon = new IconEx(IconEx.SystemIcons.Warning, new Size(10, 10))) {
e.Graphics.DrawIcon(icon.Icon, 0, 0);
}
It does look better than SystemIcons.Warning. It's squeaky clean when you use 16x16.
I want to increase font size in DateTimePicker in Window Form C#. I want to set minimum 14 to 16 font size in DateTime picker.
I have tried below code but it's not working.
dateTimePicker1.CalendarFont = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));
If you wish to retain visual styles for the other controls in your application, but disable for the date picker's drop down only, you can use the following code:
public class MyDateTimePicker : DateTimePicker
{
[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
static extern Int32 SetWindowTheme(IntPtr hWnd, String textSubAppName, String textSubIdList);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
protected override void OnDropDown(EventArgs eventargs)
{
if (Application.RenderWithVisualStyles)
{
const int DTM_GETMONTHCAL = 0x1008;
//Get handle of calendar control - disable theming
IntPtr hCalendar = SendMessage(this.Handle, DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
if (hCalendar != IntPtr.Zero)
{
SetWindowTheme(hCalendar, "", "");
//Get handle of parent popup - resize appropriately
IntPtr hParent = GetParent(hCalendar);
if (hParent != IntPtr.Zero)
{
//The size you specify here will depend on the CalendarFont size chosen
MoveWindow(hParent, 0, 0, 400, 300, true);
}
}
}
base.OnDropDown(eventargs);
}
}
In main program delete / comment line Application.EnableVisualStyles();
and add new line of code after yours:
dateTimePicker1.Font = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));
In iOS u have to make a renderer:
private void SetFont(CustomPicker view)
{
UIFont uiFont;
Control.Font = UIFont.SystemFontOfSize(11f); //the size which u want
}
In android you have to set the font for the renderer:
private void SetFont(CustomDatePicker view)
{
if (view.Font != Font.Default)
{
Control.TextSize = view.Font.ToScaledPixel();
Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets,"Roboto-Bold.ttf");
Control.Typeface = font;
Control.Typeface = view.Font.ToTypeface();
}
}
I'm building a WPF application in which I need to display document previews such as what is achievable with a DocumentViewer and DocumentPaginator. However, converting the report to XPS and loading it into a DocumentViewer has proven to be very slow when the report is large (as a common report I'll need to display is).
This lead me to start thinking that there is probably some way to start showing the first few pages of the report while the rest of the pages are being 'loaded' into the DocumentViewer -- basically loading/showing the pages as they're created.
Does anyone know if something like this is possible? And, if so, how would you suggest I get started trying to make it work? I've spent a few hours looking around online for a solution to display the report faster, but haven't come up with anything.
For the sake of full disclosure, in this case the report I need to display is being created in HTML. I know that I need to convert it to XPS in order to use the DocumentViewer, but I bring this up because if anyone has a fast way of displaying the HTML, please feel free to bring that up too. I can't use a WebBrowser control as I have to have the display in a 'print preview' type of mode. A good algorithm for deciding how to 'paginate' an HTML site would probably lead me to a solution to this problem as well as then I could create a custom control to display it. I'd use a DocumentPaginator, but then the outputted file is XPS and then I'm back to the DocumentViewer issue.
Again, any help is greatly appreciated. Thank you!
Ok, I think I've got something...
Once again I found a better URL to reference. This one wasn't loading for me straight up so I grabbed it from the Google cache: http://webcache.googleusercontent.com/search?q=cache:LgceMCkJBrsJ:joshclose.net/%3Fp%3D247
Define the IViewObject interface as described in each article:
[ComVisible(true), ComImport()]
[GuidAttribute("0000010d-0000-0000-C000-000000000046")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IViewObject
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int Draw(
[MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect,
int lindex,
IntPtr pvAspect,
[In] IntPtr ptd,
IntPtr hdcTargetDev,
IntPtr hdcDraw,
[MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcBounds,
[MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcWBounds,
IntPtr pfnContinue,
[MarshalAs(UnmanagedType.U4)] UInt32 dwContinue);
[PreserveSig]
int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect,
int lindex, IntPtr pvAspect, [In] IntPtr ptd,
IntPtr hicTargetDev, [Out] IntPtr ppColorSet);
[PreserveSig]
int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect,
int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);
[PreserveSig]
int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze);
void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects,
[In, MarshalAs(UnmanagedType.U4)] int advf,
[In, MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);
void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects,
[In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf,
[In, Out, MarshalAs(UnmanagedType.LPArray)] IAdviseSink[] pAdvSink);
}
Create an HtmlPaginator class that screenshots the browser's document (as described) but then crops it into pages / frames:
class HtmlPaginator
{
public event EventHandler<PageImageEventArgs> PageReady;
protected virtual void OnPageReady(PageImageEventArgs e)
{
EventHandler<PageImageEventArgs> handler = this.PageReady;
if (handler != null)
handler(this, e);
}
public class PageImageEventArgs : EventArgs
{
public Image PageImage { get; set; }
public int PageNumber { get; set; }
}
public void GeneratePages(string doc)
{
Bitmap htmlImage = RenderHtmlToBitmap(doc);
int pageWidth = 800;
int pageHeight = 600;
int xLoc = 0;
int yLoc = 0;
int pages = 0;
do
{
int remainingHeightOrPageHeight = Math.Min(htmlImage.Height - yLoc, pageHeight);
int remainingWidthOrPageWidth = Math.Min(htmlImage.Width - xLoc, pageWidth);
Rectangle cropFrame = new Rectangle(xLoc, yLoc, remainingWidthOrPageWidth, remainingHeightOrPageHeight);
Bitmap page = htmlImage.Clone(cropFrame, htmlImage.PixelFormat);
pages++;
PageImageEventArgs args = new PageImageEventArgs { PageImage = page, PageNumber = pages };
OnPageReady(args);
yLoc += pageHeight;
if (yLoc > htmlImage.Height)
{
xLoc += pageWidth;
if (xLoc < htmlImage.Width)
{
yLoc = 0;
}
}
}
while (yLoc < htmlImage.Height && xLoc < htmlImage.Width);
}
private static Bitmap RenderHtmlToBitmap(string doc)
{
Bitmap htmlImage = null;
using (var webBrowser = new WebBrowser())
{
webBrowser.ScrollBarsEnabled = false;
webBrowser.ScriptErrorsSuppressed = true;
webBrowser.DocumentText = doc;
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width;
webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height;
htmlImage = new Bitmap(webBrowser.Width, webBrowser.Height);
using (Graphics graphics = Graphics.FromImage(htmlImage))
{
var hdc = graphics.GetHdc();
var rect1 = new Rectangle(0, 0, webBrowser.Width, webBrowser.Height);
var rect2 = new Rectangle(0, 0, webBrowser.Width, webBrowser.Height);
var viewObject = (IViewObject)webBrowser.Document.DomDocument;
viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref rect1, ref rect2, IntPtr.Zero, 0);
graphics.ReleaseHdc(hdc);
}
}
return htmlImage;
}
}
Call it like so:
WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.stackoverflow.com");
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
HtmlPaginator pagr = new HtmlPaginator();
pagr.PageReady += new EventHandler<HtmlPaginator.PageImageEventArgs>(pagr_PageReady);
pagr.GeneratePages(browser.DocumentText);
To test it I implemented a basic form with a button and a picture box and a List collection. I add pages to the collection as they're ready from the HtmlPaginator and use the button to add the next image to the picturebox.
The magic numbers are your desired width and height. I used 800x600 but you probably have different dimensions you want.
The downside here is you're still waiting for the WebBrowser to render the HTML but I really don't see how an alternate solution is going to reduce that time - something has to interpret and draw the HTML in the first place. Write your own web browser I guess. :)
I did try playing with IViewObject.Draw to see if I could just have it render the page frames directly rather than have the cropping loop, but it wasn't working for me.