Hamid
Hamid

Reputation: 617

Empty Bitmap when converting from Graphics to Bitmap

I use a library for drawing outline text from here http://www.codeproject.com/Articles/42529/Outline-Text

I Wrote a function for test amd I tried to save outlineText Graphics object as Bitmap Graphic object draw on control Correctly but when I save it as image the result image is empty

        Graphics graphic = this .CreateGraphics();
        graphic.SmoothingMode = SmoothingMode.AntiAlias;
        StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
        Int32 lNum = (Int32)Math.Log((Double)this.TextAlign, 2);
        fmt.LineAlignment = (StringAlignment)(lNum / 4);
        fmt.Alignment = (StringAlignment)(lNum % 4);

        OutlineText m_OutlineText = new OutlineText();            

        m_OutlineText.EnableShadow(true);
        m_OutlineText.SetNullShadow();
        m_OutlineText.Shadow(ShadowColor, ShadowSize, new Point(4, 4));
        FontFamily fontFamily = this.Font.FontFamily;
        float fStartX = 0.0f;
        float fStartY = 0.0f;
        float fDestWidth = 0.0f;
        float fDestHeight = 0.0f;
        m_OutlineText.MeasureString(
            graphic,
            fontFamily,
            this.Font.Style,
            (int)this.Font.Size,
            this.Text,
            new Point(10, 10),
            fmt,
            ref fStartX,
            ref fStartY,
            ref fDestWidth,
            ref fDestHeight);

        LinearGradientBrush gradientBrush = new LinearGradientBrush(new RectangleF(fStartX, fStartY, fDestWidth - (fStartX - 10), fDestHeight - (fStartY - 10)),
           GrediantA, GrediantB, LinearGradientMode.Vertical);
        m_OutlineText.TextOutline(gradientBrush, OutlineColor, OutlineSize);
        if (_myRc == null)
        {
            _myRc = ClientRectangle;
        }
        m_OutlineText.DrawString(graphic, fontFamily, this.Font.Style, (int)this.Font.Size, this.Text, _myRc, fmt);


      _textImage = new Bitmap(this.Width, this.Height, graphic);
      _textImage.Save(@"C:\bmp.jpg");

Upvotes: 3

Views: 2890

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

Constructor: Bitmap(Int32, Int32, Graphics) initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object. It should be empty. Only resolution of Graphics will be used.

If you want to draw something on the bitmap and save that, than create graphics from bitmap:

using(Bitmap bitmap = new Bitmap(Width, Height))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
    Rectangle rect = new Rectangle(0, 0, Width, Height);
    graphics.FillRectangle(new SolidBrush(BackColor), rect);
    graphics.DrawString("Hello, World", Font, new SolidBrush(ForeColor), rect);
    bitmap.Save(@"D:\hello_world.bmp");
} 

If you want to draw on your control and save it's image to file, use Control.DrawToBitmap method.

BTW bitmap should have .bmp extension, it's not JPEG image.

Upvotes: 4

Related Questions