public void Dispose_Family () { PrivateFontCollection pfc = new PrivateFontCollection (); pfc.Dispose (); Assert.IsNotNull (pfc.Families); // no it's not a ObjectDisposedException }
private void changeToBrailleFont() { //define a private font collection System.Drawing.Text.PrivateFontCollection pfc2 = new System.Drawing.Text.PrivateFontCollection(); //read your resource font into a byte array byte[] Bytes = Properties.Resources.SimBraille; //allocate some memory and get a pointer to it IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(Bytes.Length); //copy the font data byte array to memory System.Runtime.InteropServices.Marshal.Copy(Bytes, 0, ptr, Bytes.Length); //Add the font to the private font collection pfc2.AddMemoryFont(ptr, Bytes.Length); //free up the previously allocated memory System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr); //define a font from the private font collection System.Drawing.Font fnt2 = new System.Drawing.Font(pfc2.Families[0], 16); //dispose of the private font collection pfc2.Dispose(); System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection(); string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\Resources\\"; string font = "SimBraille.ttf"; try { pfc.AddFontFile(path + font); System.Drawing.Font fnt = new System.Drawing.Font(pfc.Families[0], 16); richTextBoxEditor.Font = fnt; richTextBoxEditor.SelectionStart = 0; richTextBoxEditor.SelectionLength = richTextBoxEditor.TextLength; richTextBoxEditor.SelectionFont = fnt; richTextBoxEditor.SelectionStart = 0; richTextBoxEditor.SelectionLength = 0; int rightMargin = setRightMargin(fnt2); richTextBoxEditor.RightMargin = rightMargin; fnt.Dispose(); } catch (Exception error) { MessageBox.Show(error.Message); } //return the font created from your font resource textToolStripMenuItem.Checked = false; punktToolStripMenuItem.Checked = true; }
private void comboBoxTPFont_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxTPFont.SelectedIndex == -1) return; switch (comboBoxTPFont.SelectedIndex) { case 0: if (pfc != null) { pfc.Dispose(); pfc = null; } //PrivateFontCollectionオブジェクトを作成する pfc = new System.Drawing.Text.PrivateFontCollection(); byte[] fontBuf = null; if (comboBoxTPFont.SelectedIndex == 0) { fontBuf = Properties.Resources.pirulen_rg; } IntPtr fontBufPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontBuf.Length); System.Runtime.InteropServices.Marshal.Copy(fontBuf, 0, fontBufPtr, fontBuf.Length); pfc.AddMemoryFont(fontBufPtr, fontBuf.Length); System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontBufPtr); //PrivateFontCollectionの先頭のフォントのFontオブジェクトを作成する System.Drawing.Font f = new System.Drawing.Font(pfc.Families[0], (int)numericUpDownFontSize.Value); selectedFont = f; break; default: if (pfc != null) { pfc.Dispose(); pfc = null; } selectedFont = new Font(comboBoxTPFont.Text, (int)numericUpDownFontSize.Value); break; } controller.TPFont = selectedFont; }
public static PhpArray imagettftext(PhpResource im, double size, double angle, int x, int y, int col, string font_file, string text) { PhpGdImageResource img = PhpGdImageResource.ValidImage(im); if (img == null) return null; if (string.IsNullOrEmpty(font_file)) { PhpException.Throw(PhpError.Warning, Utils.Resources.GetString("filename_cannot_be_empty")); return null; } font_file = Path.Combine(ScriptContext.CurrentContext.WorkingDirectory, font_file); if (!File.Exists(font_file)) { PhpException.Throw(PhpError.Warning, String.Format(Utils.Resources.GetString("invalid_font_filename"), font_file)); return null; } // Font preparation PrivateFontCollection pfc; try { pfc = new PrivateFontCollection(); pfc.AddFontFile(font_file); } catch { PhpException.Throw(PhpError.Warning, String.Format(Utils.Resources.GetString("invalid_font_filename"), font_file)); return null; } FontStyle style = FontStyle.Regular; if (!pfc.Families[0].IsStyleAvailable(FontStyle.Regular)) { if (pfc.Families[0].IsStyleAvailable(FontStyle.Bold)) { style = FontStyle.Bold; } else if (pfc.Families[0].IsStyleAvailable(FontStyle.Italic)) { style = FontStyle.Italic; } else if (pfc.Families[0].IsStyleAvailable(FontStyle.Underline)) { style = FontStyle.Underline; } else if (pfc.Families[0].IsStyleAvailable(FontStyle.Strikeout)) { style = FontStyle.Strikeout; } } Font font = new Font(pfc.Families[0], (float)size, style, GraphicsUnit.Point); float descent = font.Size * font.FontFamily.GetCellDescent(style) / font.FontFamily.GetEmHeight(style); float ascent = font.Size * font.FontFamily.GetCellAscent(style) / font.FontFamily.GetEmHeight(style); Point origin = new Point(x, (int)(y - (ascent + descent))); StringFormat sf = new StringFormat(StringFormat.GenericTypographic); Graphics g = Graphics.FromImage(img.Image); //graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; SizeF sizef = g.MeasureString(text, font, origin, sf); System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix(); matrix.RotateAt(-(float)angle, new PointF(x + (sizef.Width / 2), y - ((ascent + descent) / 2))); Point[] points = { origin }; matrix.TransformPoints(points); int difX = (origin.X - points[0].X); int difY = (origin.Y - points[0].Y); origin.X = origin.X - difX; origin.Y = origin.Y + difY; g.Transform = matrix; //g.TranslateTransform(origin.X - points[0].X, origin.Y - points[0].Y); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; Color color = GetAlphaColor(img, col); SolidBrush brush = new SolidBrush(color); g.DrawString(text, font, brush, origin, sf); brush.Dispose(); sf.Dispose(); font.Dispose(); pfc.Dispose(); g.Dispose(); Point[] points2 = new Point[4]; if (angle != 0) { points2[0] = new Point(x - difX, y + difY + 2); points2[1] = new Point((int)(x - difX + sizef.Width), y + difY + 2); points2[2] = new Point((int)(x - difX + sizef.Width), (int)(y - (ascent + descent)) + difY + 2); points2[3] = new Point((int)(x - difX), (int)(y - (ascent + descent)) + difY + 2); matrix.TransformPoints(points2); } else { points2[0] = new Point(x - difX, y + difY); points2[1] = new Point((int)(x - difX + sizef.Width), y + difY); points2[2] = new Point((int)(x - difX + sizef.Width), (int)(y - (ascent + descent)) + difY + 3); points2[3] = new Point((int)(x - difX), (int)(y - (ascent + descent)) + difY + 3); } PhpArray array = new PhpArray(); array.Add(points2[0].X); array.Add(points2[0].Y); array.Add(points2[1].X); array.Add(points2[1].Y); array.Add(points2[2].X); array.Add(points2[2].Y); array.Add(points2[3].X); array.Add(points2[3].Y); return array; }
protected static void AddText(int x, int y, string text, Graphics graphics, TextStyle cls, StringFormat format) { if (cls == null) throw new ArgumentNullException("cls"); lock (FontLock) { Font verFont; PrivateFontCollection pfc = new PrivateFontCollection(); try { if (cls.FontFileName != null && cls.FontFileName != string.Empty) { try { pfc.AddFontFile(cls.FontFileName); } catch (ArgumentNullException ex) { throw new Exception(string.Format("Font with filename '{0}' not found", cls.FontFileName), ex); } verFont = new Font(pfc.Families[0], cls.Size, cls.Style, GraphicsUnit.Pixel); } else verFont = new Font(cls.Family, cls.Size, cls.Style, GraphicsUnit.Pixel); using (verFont) { graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.DrawString(text, verFont, cls.Brush, x, y, format); } } finally { // due to a bug in the BCL, each fontfamily object in the private font collection has to // be explicitly disposed. See: // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=202970 // http://support.microsoft.com/kb/901026 // http://blog.loseyourmind.com/?m=200710 if (pfc.Families.Length > 0) { for (int i = pfc.Families.Length - 1; i >= 0; i--) pfc.Families[i].Dispose(); pfc.Dispose(); } } } }
public void addFonts(string[] filePaths, bool copy=true) { foreach (string fileName in filePaths) { //Console.WriteLine(fileName); //Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); FileInfo info = new FileInfo(fileName); // If the font list already contains this font, just skip it // TODO: This will only check file name. It would be nice if we // could either open the font file and check font name, or exclude // extension from the check, so that you can't add an OTF and TTF of // the same font, for example. // TODO: If we *do* detect a duplicate? Perhaps show a dialog asking the // user what we should do? if (listFonts.FindItemWithText(info.Name) != null) continue; if (copy) { try { File.Copy(fileName, Path.Combine(APP_DATA, info.Name), true); } catch (Exception ex) { Console.WriteLine("Exception occured: " + ex.ToString()); MessageBox.Show(String.Format("Error copying {0} into fonts directory.", fileName), "Error copying file", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } PrivateFontCollection tempCollection = new PrivateFontCollection(); tempCollection.AddFontFile(Path.Combine(APP_DATA, info.Name)); fonts.AddFontFile(Path.Combine(APP_DATA, info.Name)); // Create the new item to add to the list string[] data = {tempCollection.Families[0].Name, info.Name}; ListViewItem newItem = new ListViewItem(data); newItem.Checked = true; // TODO: User configurable font size. // TODO: It seems that the font goes away if you select the item before // the font is installed. Should I just automatically install fonts? newItem.Font = new Font(tempCollection.Families[0], 12); listFonts.Items.Add(newItem); tempCollection.Dispose(); } }