Ultrabutton not displaying unicode character in text property while running application - c#

I'm trying to display the text property of a UltraButton to this unicode character- ▼ .
I've tried to copy this from the Character Map and also tried something like-
button1.Text = "\u2129"
The problem is both of them show the arrows in the designer mode in VS, but when I run the application, it shows an unrecognised symbol. I've gone through this link and this link, but the arrows only show up in the designer view, not while running the application. Why is this happening. Also, I've set the Font name to 'Arial Unicode MS'

I'm guessing the issue you are experiencing is unique to the UltraButton. From the looks of the image you just posted, you could probably get away with just using a standard Windows From Button. If you can, just open your ClassName.Designer.cs and find where your button text is being set. Copy the actual character into the text string:
this.YourButton.Text = "▼";
This shows up correctly in both the designer and when running the application.
If you really don't want to use a standard Windows Forms Button, you could always go about converting your text to an image and adding the image to the button. Would look something like this:
string text = "▼";
Font font = new Font("Arial Unicode MS", 12f);
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
SizeF textsize = drawing.MeasureString(text, font);
img.Dispose();
drawing.Dispose();
img = new Bitmap((int) textsize.Width, (int)textsize.Height);
drawing = Graphics.FromImage(img);
drawing.Clear(YourButton.BackColor);
Brush textBrush = new SolidBrush(Color.Black);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
YourButton.Text = "";
YourButton.Image = img;

Related

Draw text to image change bug [duplicate]

This question already has answers here:
Drawing aligned strings by formatting double values
(3 answers)
How to draw a table using System.Drawing
(1 answer)
Graphics.DrawString() & leading padding spaces for Proportional Font
(1 answer)
How to create control that draws a table in panel
(1 answer)
Closed 1 year ago.
I want to create program that draws text on image. For that purpose I used this method:
private static Image DrawText(String text, Font font, Color textColor, Color backColor)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size
img = new Bitmap((int)textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//paint the background
drawing.Clear(backColor);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
My problem is that I want to draw table in the image, so the table should look like:
I used this library for drawing table
And code looks like this method from above
var table = new ConsoleTable("one", "two", "three")
.AddRow("random text';", "random text", "random text")
.Configure(o => o.NumberAlignment = Alignment.Left)
.ToString();
DrawText(table, new Font("Verdana", 20), Color.Black, Color.White);
And i got this kind of image
Rows are really changed and it doesn't look like table above. I think method DrawText changed something but I don't know what exactly it is? So I need help. Sorry for my bad English
If you don't need Verdana (which isn't a monospaced font), you should be able to fix this by slightly altering your call to DrawText like so:
DrawText(table, new Font(FontFamily.GenericMonospace, 20), Color.Black, Color.White);
I'm not actually super familiar with working with fonts, so using FontFamily.GenericMonospace is sort of my best guess. You should be able to use others though. Wikipedia has a list of them.

SVG: measure the bounding box of some text from C#

I am writing a program that is going to dump an SVG file, in C#. I was wondering if there is any way of measuring how big a piece of text is going to be from the C# program.
I can assume that I know the font and fontsize. But the "average size of letter"*number_of_letters is very inaccurate.
I am looking for some option like this:
1) Just figure out, from font size and font, how big the text is.
2) I could dump the SVG once, and get the measure from there (by rendering it somehow? and then reading that information from it, somehow?)
Thanks!
Figured it out:
Font stringFont = new Font("Verdana", 8, GraphicsUnit.Pixel);
Image newImage = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(newImage);
SizeF stringSize = new SizeF();
g.PageUnit = GraphicsUnit.Pixel;
stringSize = g.MeasureString("Hello, this is \n a string and stuff", stringFont);

Add watermark below image but within print area in C#

I am trying to insert a text watermark underneath a TIFF image in my windows form and would definitely appreciate anyone's help. I have a print button that retrieves the image, scales it down, then based on my margins, places the image accordingly to print. I'd like to add an additional piece where just before the image prints, I add in a text watermark (in this case a date stamp) that is just below the image.
I've tried adjusting the margin but that just increases (or decreases depending on the number setting) the image scale but does not add the additional room I want to add the watermark. Below is code of what I have so far:
protected void PrintPage(object sender, PrintPageEventArgs e)
{
if (this.Image == null)
{
e.Cancel = true;
return;
}
//ADD TIME STAMP WATERMARK
string watermark = "DATE ISSUED: " + String.Format("{0:MM/dd/yyyy}", System.DateTime.Now.Date);
System.Drawing.Graphics gpr = Graphics.FromImage(Image);
System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);
Font font = new System.Drawing.Font("Arial", 55, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
SizeF size = e.Graphics.MeasureString(watermark, font);
float x = 0;
float y = Image.Height-size.Height;
RectangleF printArea = new RectangleF(x, y, size.Width, size.Height);
gpr.DrawString(watermark, font, brush, printArea);
e.Graphics.DrawImage(this.Image, e.MarginBounds);
}
The value of e.MarginBounds I have set in my App.config and include the following values: Left=70, Right=90, Top=190; Bottom=475. All the printouts are going to be printed portrait style on Letter 8 1/2 by 11 size paper.
I am able to display the watermark anywhere on top of the image, but I am hoping to place it underneath. When I adjust the y coordinate, and it so happens to be below the image, when I print, I assume that it is outside the print area and therefore, the watermark does not get printed on the page (it only shows the image).
I appreciate anyone's help in this as I have been racking my brain on this and have had no luck.
Aren't you printing your text beneath the image. I think you want to start printing at y=Image.Height + e.MarginBounds.Top, and x=e.MarginBounds.Left
That will print a your label left justified below the image in the margin.
Update: This works:
y=-size.Height + e.MarginBounds.Bottom;
x = e.MarginBounds.Left;
e.Graphics.DrawImage(Image, e.MarginBounds);
// note the change. Using e.graphics instead of gpr below
e.Graphics.DrawString(watermark, font, brush, printArea);

Generating tiff file from a multi-line text box in C#

I am writing info entered on a web form into a tiff file. My issue is where the comment box comes into play for the web form. The comment box is multi-line and when writing it on to the tiff file, some of the information entered into the comment box falls out of the tiff image.
The code for how I am trying to write it to the tiff file:-
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); //Creates Bitmap
Graphics g = Graphics.FromImage(bitmap);
g.DrawString("Comment: " + CommentBox.Text, outputFont, Brushes.Black, new PointF(0, 700)); // Writing the text from the comment box on to the Tiff file.
so what that means to me is, if I am writing a multi line comment as:-
"Hello Testing.
Hello Testing again and again and again and again and again and again and again.
Hello Testing again and again and again and again and again and again and again. Hello Testing again and again and again and again and again and again and again."
My tiff-file captures line elements only upto the 1000 width, the elements beyond that width do not automatically get generated in a new line.
Can anyone help me with this? ideas?
Try something like this - you can use the overload of DrawString that takes a containing rectangle, and the text should wrap within that:
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); //Creates Bitmap
using(Graphics g = Graphics.FromImage(bitmap))
{
RectangleF rect = new RectangleF(new PointF(0, 700), new SizeF(200,200)); // adjust these accordingly for your bounding rect
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Near;
g.DrawString("Comment: " + CommentBox.Text, outputFont, Brushes.Black, rect, drawFormat); // Writing the text from the comment box on to the Tiff file.
}

Icon to Image - transparency issue

I'm trying to build a treeview like file list in a richtext box.
It should look like an explorer treeview. My code is able to get an resize the icon, but the transparency is missing (light gray background instead of transparency). What do I need to change here? Is the Image format wrong?
Is there a better way to add an image to a richtextbox?
// Get file info
FileInfo f = new FileInfo("myfile.name");
// Get icon for fileinfo
Icon ico = Icon.ExtractAssociatedIcon(f);
// Convert icon to bitmap
Bitmap bm = ico.ToBitmap();
// create new image with desired size
Bitmap img = new Bitmap(16,16,PixelFormat.Frmat32bpRgb);
// Create graphics with desired sized image
Graphics g = Graphics.FormImage(img);
// set interpolation mode
g.InterpolationMode = InterpolationMode.HighQualityBiCubic;
// draw/resize image
g.DrawImage(bm, new Rectangle(0,0,16,16), new Rectangle(0, 0, bm.Width, bm,Height), GraphicalUnit.Pixel);
// Paste to clipboard
Clipboard.SetImage(bm);
// Paste in RichtextBox
rtb.Paste();
Example:
Edit:
I've figured out that the image is transparent, but using Clipboard.SetImage() doesn't publish it as transparent image.
Any ideas why and what can I do to fix it? Do I need to switch to a differn textbox control?
I've had some luck going through Graphics.
Bitmap b = new Bitmap(pbAssetLoaded.Width, pbAssetLoaded.Height);
using (Graphics g = Graphics.FromImage(b))
{
g.DrawIcon(SystemIcons.Information, 0, 0);
}
This draws the icon with transparency to the Bitmap.
Try
img.MakeTransparent();
after you contruct it.
Note that this will change your PixelFormat to Format32bppArgb.

Categories

Resources