2323
2424#endregion
2525
26- using System . Drawing . Drawing2D ;
26+ using System . Drawing ;
27+ using System . Drawing . Imaging ;
2728using MiNET . Net ;
2829using MiNET . Utils ;
29- using SixLabors . Fonts ;
30- using SixLabors . ImageSharp ;
31- using SixLabors . ImageSharp . Drawing . Processing ;
32- using SixLabors . ImageSharp . PixelFormats ;
33- using SixLabors . ImageSharp . Processing ;
34- using Color = System . Drawing . Color ;
35- using RectangleF = System . Drawing . RectangleF ;
3630
3731namespace MiNET . Entities . ImageProviders
3832{
3933 public class TextMapImageProvider : IMapImageProvider
4034 {
41- private static FontCollection _fontCollection ;
4235 private static Font _font = null ;
4336
4437 static TextMapImageProvider ( )
4538 {
46- _fontCollection = new FontCollection ( ) ;
47- _fontCollection . AddSystemFonts ( ) ;
48-
49- if ( _fontCollection . TryGet ( "Arial" , out var family ) )
50- {
51- _font = family . CreateFont ( 9 ) ;
52- }
39+ _font = new Font ( "Arial" , 9 ) ;
5340 }
5441
5542 public string Text { get ; set ; }
@@ -91,36 +78,39 @@ public virtual McpeWrapper GetBatch(MapInfo mapInfo, bool forced)
9178
9279 private static byte [ ] DrawText ( MapInfo map , string text )
9380 {
94- var bitmap = new Image < Rgba32 > ( map . Col , map . Row ) ;
95- var rectf = new RectangleF ( 0 , 0 , map . Col , map . Row ) ;
96- /*var g = Graphics.FromImage(bitmap);
97- g.SmoothingMode = SmoothingMode.AntiAlias;
98- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
99- g.PixelOffsetMode = PixelOffsetMode.HighQuality;
100- g.DrawString(text, new Font("SketchFlow Print", 10), Brushes.AntiqueWhite, rectf);
101- g.Flush();*/
102-
103- bitmap . Mutate (
104- x =>
81+ using ( var bitmap = new Bitmap ( map . Col , map . Row ) )
82+ {
83+ using ( Graphics graphics = Graphics . FromImage ( bitmap ) )
10584 {
106- x . DrawText ( text , _font , SixLabors . ImageSharp . Color . AntiqueWhite , new PointF ( 0 , 0 ) ) ;
107- } ) ;
85+ graphics . TextRenderingHint = System . Drawing . Text . TextRenderingHint . AntiAlias ;
86+ graphics . DrawString ( text , _font , Brushes . AntiqueWhite , new PointF ( 0 , 0 ) ) ;
87+ }
10888
109- byte [ ] bytes = new byte [ bitmap . Height * bitmap . Width * 4 ] ;
89+ Rectangle rect = new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ;
90+ BitmapData bmpData = bitmap . LockBits ( rect , ImageLockMode . ReadOnly , bitmap . PixelFormat ) ;
11091
111- int i = 0 ;
112- for ( int y = 0 ; y < bitmap . Height ; y ++ )
113- {
114- for ( int x = 0 ; x < bitmap . Width ; x ++ )
92+ int bytesPerPixel = Bitmap . GetPixelFormatSize ( bitmap . PixelFormat ) / 8 ;
93+ int byteCount = bmpData . Stride * bitmap . Height ;
94+ byte [ ] bytes = new byte [ byteCount ] ;
95+
96+ System . Runtime . InteropServices . Marshal . Copy ( bmpData . Scan0 , bytes , 0 , byteCount ) ;
97+
98+ bitmap . UnlockBits ( bmpData ) ;
99+
100+ for ( int i = 0 ; i < bytes . Length ; i += bytesPerPixel )
115101 {
116- var color = bitmap [ x , y ] ;
117- bytes [ i ++ ] = color . R ;
118- bytes [ i ++ ] = color . G ;
119- bytes [ i ++ ] = color . B ;
120- bytes [ i ++ ] = 0xff ;
102+ byte b = bytes [ i ] ;
103+ byte g = bytes [ i + 1 ] ;
104+ byte r = bytes [ i + 2 ] ;
105+ byte a = bytes [ i + 3 ] ;
106+ bytes [ i ] = r ;
107+ bytes [ i + 1 ] = g ;
108+ bytes [ i + 2 ] = b ;
109+ bytes [ i + 3 ] = a ;
121110 }
111+
112+ return bytes ;
122113 }
123- return bytes ;
124114 }
125115 }
126116}
0 commit comments