-
-
Save naveedmurtuza/6600103 to your computer and use it in GitHub Desktop.
| /// <summary> | |
| /// Converting text to image (png). | |
| /// </summary> | |
| /// <param name="text">text to convert</param> | |
| /// <param name="font">Font to use</param> | |
| /// <param name="textColor">text color</param> | |
| /// <param name="maxWidth">max width of the image</param> | |
| /// <param name="path">path to save the image</param> | |
| public static void DrawText(String text, Font font, Color textColor,int maxWidth,String path) | |
| { | |
| //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,maxWidth); | |
| //set the stringformat flags to rtl | |
| StringFormat sf = new StringFormat(); | |
| //uncomment the next line for right to left languages | |
| //sf.FormatFlags = StringFormatFlags.DirectionRightToLeft; | |
| sf.Trimming = StringTrimming.Word; | |
| //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); | |
| //Adjust for high quality | |
| drawing.CompositingQuality = CompositingQuality.HighQuality; | |
| drawing.InterpolationMode = InterpolationMode.HighQualityBilinear; | |
| drawing.PixelOffsetMode = PixelOffsetMode.HighQuality; | |
| drawing.SmoothingMode = SmoothingMode.HighQuality; | |
| drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
| //paint the background | |
| drawing.Clear(Color.Transparent); | |
| //create a brush for the text | |
| Brush textBrush = new SolidBrush(textColor); | |
| drawing.DrawString(text, font, textBrush, new RectangleF(0, 0, textSize.Width,textSize.Height), sf); | |
| drawing.Save(); | |
| textBrush.Dispose(); | |
| drawing.Dispose(); | |
| img.Save(path,ImageFormat.Png); | |
| img.Dispose(); | |
| } |
thanks much
Thanks, great sample!
Thanks It working very well.
working fine.Thanks
Hi,
I am trying to use this script in unity. I am trying to achieve the same. I want to create an image with my Text (string).
I am getting this error. Can any one please help me to resolve this.
Thanks in Advance!
DllNotFoundException: /Users/bokken/build/output/Unity-Technologies/mono/external/buildscripts/add_to_build_results/monodistribution/lib/libgdiplus.dylib assembly:<unknown assembly> type:<unknown type> member:(null) System.Drawing.GDIPlus..cctor () (at <6237fc0e9e0548509b13786c3fddb07c>:0) Rethrow as TypeInitializationException: The type initializer for 'System.Drawing.GDIPlus' threw an exception. System.Drawing.Bitmap..ctor (System.Int32 width, System.Int32 height, System.Drawing.Imaging.PixelFormat format) (at <6237fc0e9e0548509b13786c3fddb07c>:0) System.Drawing.Bitmap..ctor (System.Int32 width, System.Int32 height) (at <6237fc0e9e0548509b13786c3fddb07c>:0) (wrapper remoting-invoke-with-check) System.Drawing.Bitmap..ctor(int,int) TextToImage.DrawText (System.String text, System.Drawing.Font font, System.Drawing.Color textColor, System.Int32 maxWidth, System.String path) (at Assets/Scripts/Utils/TextToImage.cs:21)
I have no experience with this, but this thread seems to deal with your problem:
https://discussions.unity.com/t/how-do-you-load-system-drawing-dll-and-gdiplus-dll-on-unity-mac/27248/4
Hi, I am trying to use this script in unity. I am trying to achieve the same. I want to create an image with my Text (string). I am getting this error. Can any one please help me to resolve this. Thanks in Advance!
DllNotFoundException: /Users/bokken/build/output/Unity-Technologies/mono/external/buildscripts/add_to_build_results/monodistribution/lib/libgdiplus.dylib assembly:<unknown assembly> type:<unknown type> member:(null) System.Drawing.GDIPlus..cctor () (at <6237fc0e9e0548509b13786c3fddb07c>:0) Rethrow as TypeInitializationException: The type initializer for 'System.Drawing.GDIPlus' threw an exception. System.Drawing.Bitmap..ctor (System.Int32 width, System.Int32 height, System.Drawing.Imaging.PixelFormat format) (at <6237fc0e9e0548509b13786c3fddb07c>:0) System.Drawing.Bitmap..ctor (System.Int32 width, System.Int32 height) (at <6237fc0e9e0548509b13786c3fddb07c>:0) (wrapper remoting-invoke-with-check) System.Drawing.Bitmap..ctor(int,int) TextToImage.DrawText (System.String text, System.Drawing.Font font, System.Drawing.Color textColor, System.Int32 maxWidth, System.String path) (at Assets/Scripts/Utils/TextToImage.cs:21)
@mihafreenode Thanks a lot for the reply. Sure! Let me check this thread.
Not worked the above thread solutions for me. any other solution anyone?
Now I am getting this error.
DllNotFoundException: /Library/Frameworks/Mono.framework/Versions/6.12.0/lib/libgdiplus.dylib assembly:<unknown assembly> type:<unknown type> member:(null) System.Drawing.GDIPlus..cctor () (at <6237fc0e9e0548509b13786c3fddb07c>:0) Rethrow as TypeInitializationException: The type initializer for 'System.Drawing.GDIPlus' threw an exception. System.Drawing.Bitmap..ctor (System.Int32 width, System.Int32 height, System.Drawing.Imaging.PixelFormat format) (at <6237fc0e9e0548509b13786c3fddb07c>:0) System.Drawing.Bitmap..ctor (System.Int32 width, System.Int32 height) (at <6237fc0e9e0548509b13786c3fddb07c>:0) (wrapper remoting-invoke-with-check) System.Drawing.Bitmap..ctor(int,int) TextToImage.DrawText (System.String text, System.Drawing.Font font, System.Drawing.Color textColor, System.Int32 maxWidth, System.String path) (at
Thanks!
Thanks for the code.