How to flip a string?
How can I flip a string in an application c#
like for example: " AMBULANCE " which is treated as a mirror image. I have no idea where to start with .im using c#
forms
I tried changing the string, but is there a way to reverse the string (like mirrored images)?
How: ƎƆA⅃UᙠMA
This does not reverse as in SO post
Similar to the approach described by Sergey, you can also use Graphics.ScaleTransform()
to reflect everything about the Y axis.
For example, create a default Windows Forms application and put the following in it OnPaint()
, then run it:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
string text = "This will come out backwards";
e.Graphics.ScaleTransform(-1, 1);
float w = e.Graphics.MeasureString(text, this.Font).Width;
e.Graphics.DrawString(text, this.Font, Brushes.Black, -w, 0);
}
Output:
You can also use Graphics.RotateTransform()
to rotate text and also use Graphics.ScaleTransform(1, -1)
to invert it as well as mirror it:
You can create a bitmap, write a string to it, and then flip the bitmap horizontally using the Image.RotateFlip method:
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
var g = Graphics.FromImage(bitmap);
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
g.DrawString("AMBULANCE", font, brush, pictureBox.DisplayRectangle);
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
pictureBox.Image = bitmap; // display bitmap on your form
Result:
Summary: Winforms uses Unicode encoding and you can use reversed characters. To do this, you should create a dictionary with normal and inverted characters and use it after the reverse.
var dict = new Dictionary<char, char>
{
{'A','A'},
{'B','ᙠ'},
{'C','Ɔ'},
{'D','ᗡ'},
// etc.
};
var str = "ABcd";
var mirror = new string(str.ToUpper().Reverse().Select(x=>dict[x]).ToArray());
There are two parts to achieving this goal.
First, the easy part (assuming you are only dealing with Latin characters) string.Reverse()
will change the order of the characters. Otherwise, use a technique like the answer to another question .
The second part has two possible solutions. Or grab yourself a backsplash font ( free ones exist ) and show the backsplash using that. Or use the site http://txtn.us/mirror-words to create a list of characters that look like backwards and then set up a dictionary to map in between, for example
var Dictionary<char, char> = new Dictionary<char, char>
{
...
{ 'e', 'ɘ' },
...
};
Then you can perform character-by-character replacement and display that string. Obviously this will only work if your chosen font has those characters in it.