4

How can i flip a string in c# application like for example : "AMBULANCE" text that is seen as mirror image.I dont have any idea to where to start with .im using c# forms application

I have tried for reversing the string ,but is there any possibility to flip a string (like mirror images)?

Like : ƎƆИA⅃UᙠMA

This is not reversing as in SO post

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77
  • 5
    What are you using to draw the text? just a console? window forms, WPF? OpenGL, DirectX ... you can do it but we need to know the technology. – Alex Anderson Jun 01 '15 at 08:00
  • 2
    What are you using to draw the string? WPF, WinForms, Console? OpenGL? – Alex Anderson Jun 01 '15 at 08:03
  • 1
    @utility `string` is just a sequence of byte values like `[45, 23, 48]`. What you are trying to do is changing the way these bytes are displayed somewhere. Without knowing where you display string we cannot help you – Sergey Berezovskiy Jun 01 '15 at 08:05
  • 1
    You should create dictionary with normal and flipped characters and use it after reverse. – Alexey Nis Jun 01 '15 at 08:06
  • 1
    @AlexeyNis has the right idea. You can use a site like this to build up a list of flipped characters by typing in the alphabet: http://txtn.us/mirror-words the other option is of course to use an image as others have suggested – samgak Jun 01 '15 at 08:07
  • 1
    ¿ʎןןɐɔıʇɹǝʌ sɹǝʇɔɐɹɐɥɔ ǝɥʇ buıddıןɟ ʇnoqɐ ʍoɥ – fubo Jun 01 '15 at 08:08
  • I don't see any pattern usage (and I think it's normal there is **not** one) between `ƎƆ` characters (which they are `U+018E` and `U+0186` in order) and `CE` which they are `U+0043` and `U+0045`. – Soner Gönül Jun 01 '15 at 08:08

4 Answers4

7

As well as the approach outlined by Sergey, you can also use Graphics.ScaleTransform() to reflect everything about the Y-axis.

For example, create a default Windows Forms application and drop the following OnPaint() into it, 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:

The text reflected about Y

You can also mess around with Graphics.RotateTransform() to rotate the text as well, and use Graphics.ScaleTransform(1, -1) to invert it as well as mirror it:

enter image description here

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
4

You can create bitmap, write string into it and then flip bitmap horizontally with 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:

enter image description here

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • thank you @Sergey for your time..but rather using images would like to go with strings/texts – Tharif Jun 01 '15 at 08:48
  • @utility why? That's just an in-memory bitmap (2-dementional array of pixels). With WinForms any string will be finally drawn to GDI drawing surface. It doesn't matter whether it's your control or in-memory bitmap. – Sergey Berezovskiy Jun 01 '15 at 09:02
  • @utility The only way to use just strings would be to use a special reversed font and then reverse the string before displaying it in that font. – Matthew Watson Jun 01 '15 at 09:55
1

Summary: Winforms use Unicode encoding and you can use "flipped" characters. For this purpose you should create Dictionary with normal and flipped characters and use it after 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());
Alexey Nis
  • 471
  • 3
  • 9
0

There are two parts to achieving this.

First, the easy part (assuming you are only dealing with Latin-based characters), string.Reverse() will reverse the order of the characters. Otherwise use a technique such as this from an answer to another question.

The second part has two possible solutions. Either grab yourself a reversed-character font (free ones exist) and display the reversed string using that. Or, use a site such as http://txtn.us/mirror-words to build up a list of characters that look like reversed ones and then set up a dictionary to map between them, eg

var Dictionary<char, char> = new Dictionary<char, char>
{
    ...
    { 'e', 'ɘ' },
    ...
};

Then you can do a character-by-character replacement and display that string. Obviously that will only work if your chosen font has those characters in it.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131