2323
2424#endregion
2525
26- using System . Drawing ;
27- using System . Drawing . Imaging ;
2826using MiNET . Net ;
2927using MiNET . Utils ;
28+ using SixLabors . Fonts ;
29+ using SixLabors . ImageSharp ;
30+ using SixLabors . ImageSharp . Drawing . Processing ;
31+ using SixLabors . ImageSharp . PixelFormats ;
32+ using SixLabors . ImageSharp . Processing ;
33+ using RectangleF = System . Drawing . RectangleF ;
3034
3135namespace MiNET . Entities . ImageProviders
3236{
3337 public class TextMapImageProvider : IMapImageProvider
3438 {
39+ private static FontCollection _fontCollection ;
3540 private static Font _font = null ;
36-
41+
3742 static TextMapImageProvider ( )
3843 {
39- _font = new Font ( "Arial" , 9 ) ;
44+ _fontCollection = new FontCollection ( ) ;
45+ _fontCollection . AddSystemFonts ( ) ;
46+
47+ if ( _fontCollection . TryGet ( "Arial" , out var family ) )
48+ {
49+ _font = family . CreateFont ( 9 ) ;
50+ }
4051 }
41-
52+
4253 public string Text { get ; set ; }
4354
4455 public TextMapImageProvider ( string text = "" )
@@ -78,39 +89,30 @@ public virtual McpeWrapper GetBatch(MapInfo mapInfo, bool forced)
7889
7990 private static byte [ ] DrawText ( MapInfo map , string text )
8091 {
81- using ( var bitmap = new Bitmap ( map . Col , map . Row ) )
82- {
83- using ( Graphics graphics = Graphics . FromImage ( bitmap ) )
84- {
85- graphics . TextRenderingHint = System . Drawing . Text . TextRenderingHint . AntiAlias ;
86- graphics . DrawString ( text , _font , Brushes . AntiqueWhite , new PointF ( 0 , 0 ) ) ;
87- }
88-
89- Rectangle rect = new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ;
90- BitmapData bmpData = bitmap . LockBits ( rect , ImageLockMode . ReadOnly , bitmap . PixelFormat ) ;
91-
92- int bytesPerPixel = Bitmap . GetPixelFormatSize ( bitmap . PixelFormat ) / 8 ;
93- int byteCount = bmpData . Stride * bitmap . Height ;
94- byte [ ] bytes = new byte [ byteCount ] ;
92+ var bitmap = new Image < Rgba32 > ( map . Col , map . Row ) ;
93+ var rectf = new RectangleF ( 0 , 0 , map . Col , map . Row ) ;
9594
96- System . Runtime . InteropServices . Marshal . Copy ( bmpData . Scan0 , bytes , 0 , byteCount ) ;
95+ bitmap . Mutate (
96+ x =>
97+ {
98+ x . DrawText ( text , _font , SixLabors . ImageSharp . Color . AntiqueWhite , new PointF ( 0 , 0 ) ) ;
99+ } ) ;
97100
98- bitmap . UnlockBits ( bmpData ) ;
101+ byte [ ] bytes = new byte [ bitmap . Height * bitmap . Width * 4 ] ;
99102
100- for ( int i = 0 ; i < bytes . Length ; i += bytesPerPixel )
103+ int i = 0 ;
104+ for ( int y = 0 ; y < bitmap . Height ; y ++ )
105+ {
106+ for ( int x = 0 ; x < bitmap . Width ; x ++ )
101107 {
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 ;
108+ var color = bitmap [ x , y ] ;
109+ bytes [ i ++ ] = color . R ;
110+ bytes [ i ++ ] = color . G ;
111+ bytes [ i ++ ] = color . B ;
112+ bytes [ i ++ ] = 0xff ;
110113 }
111-
112- return bytes ;
113114 }
115+ return bytes ;
114116 }
115117 }
116118}
0 commit comments