Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Font f = GetSystemConfiguredFont();
- //this sets the text to be rotated 90deg clockwise (i.e. down)
- StringFormat stringFormat = new StringFormat { FormatFlags = StringFormatFlags.DirectionVertical };
- SizeF size;
- // creates 1Kx1K image buffer and uses it to find out how bit the image needs to be to fit the text
- using ( Image imageg = (Image) new Bitmap( 1000, 1000 ) )
- size = Graphics.FromImage( imageg ).
- MeasureString( text, f, 25, stringFormat );
- using ( Bitmap image = new Bitmap( (int) size.Width, (int) size.Height ) )
- {
- Graphics g = Graphics.FromImage( (Image) image );
- g.FillRectangle( Brushes.White, 0f, 0f, image.Width, image.Height );
- g.TranslateTransform( image.Width, image.Height );
- g.RotateTransform( 180.0F ); //note that we need the rotation as the default is down
- // draw text
- g.DrawString( text, f, Brushes.Black, 0f, 0f, stringFormat );
- //make be background transparent - this will be an index (rather than an alpha) transparency
- image.MakeTransparent( Color.White );
- //note that this image has to be a PNG, as GDI+'s gif handling renders any transparency as black.
- context.Response.AddHeader( "ContentType", "image/png" );
- using ( MemoryStream memStream = new MemoryStream() )
- {
- image.Save( memStream, ImageFormat.Png );
- memStream.WriteTo( context.Response.OutputStream );
- }
- }
- using ( Bitmap image = new Bitmap( (int) size.Width, (int) size.Height, PixelFormat.Format32bppArgb ) )
- {
- Graphics g = Graphics.FromImage( (Image) image );
- g.TranslateTransform( image.Width, image.Height );
- g.RotateTransform( 180.0F ); //note that we need the rotation as the default is down
- // draw text
- g.DrawString( text, f, Brushes.Black, 0f, 0f, stringFormat );
- //note that this image has to be a PNG, as GDI+'s gif handling renders any transparency as black.
- context.Response.AddHeader( "ContentType", "image/png" );
- using ( MemoryStream memStream = new MemoryStream() )
- {
- //note that context.Response.OutputStream doesn't support the Save, but does support WriteTo
- image.Save( memStream, ImageFormat.Png );
- memStream.WriteTo( context.Response.OutputStream );
- }
- }
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
- Graphics g = Graphics.FromImage( (Image) image );
Add Comment
Please, Sign In to add comment