jas_cart
jas_cart

Reputation: 43

How to create image from bitmap and save it

Here is sample code that I'm tryin to make work, but no luck so far.

            Bitmap bitmap = new Bitmap((Stream)Cache["images"]);
            Graphics g = Graphics.FromImage(bitmap);

            StringFormat strFrmt = new StringFormat();
            strFrmt.Alignment = StringAlignment.Center;

            SolidBrush btmForeColor = new SolidBrush(Color.Green);
            SolidBrush btmBackColor = new SolidBrush(Color.Black);

            Font btmFont = new Font("Verdana",7);
            SizeF textSize = new SizeF();
            textSize = g.MeasureString("Copyright", btmFont);

            float x = ((float) bitmap.Width - textSize.Width - 3);
            float y = ((float) bitmap.Height - textSize.Height - 3);
            float w = ((float) x + textSize.Width);
            float h = ((float) y + textSize.Height);

            RectangleF textArea = new RectangleF(x,y,w,h);
            g.FillRectangle(btmBackColor,textArea);

            g.DrawString("Copyright",btmFont,btmForeColor,textArea);
            btmForeColor.Dispose();
            btmBackColor.Dispose();
            btmFont.Dispose();
            g.Dispose();

As you can see from the code, I'm getin Stream, and creatin bitmap, then making some changes upon bitmap, and now I want to save my bitmap object, but can't figure out how, I made some research in internet, but all the examples/articles/forum posts were for cases when you have some image file on the server, and want to make changes, in my case, I just have some stream, and want to save bitmap object in specific path. How can I do this ? Any kind of help would be appreciated.

Upvotes: 0

Views: 6817

Answers (2)

jas_cart
jas_cart

Reputation: 43

seems I found a solution

var photoPath = Server.MapPath("~/" + AsyncFileUpload1.FileName);


            if (File.Exists(photoPath))
            {
                File.Delete(photoPath);
            }
using (var mainFile = File.Create(photoPath))
            {
                // dostuff()
                bitmap.Save(mainFile, image.RawFormat);

            }

I had to create file before saving it. Thank you Mike for beeing with me in this, I'm really appreciated!!!

Upvotes: 0

Mike Miller
Mike Miller

Reputation: 16575

Surely its just bitmap.save? or one of its overloads? g is really drawing on the bitmap.

** update

        Bitmap bitmap = new Bitmap(@"C:\Users\mike\Pictures\Panasonic\P1000016.jpg");
        Graphics g = Graphics.FromImage(bitmap);

        StringFormat strFrmt = new StringFormat();
        strFrmt.Alignment = StringAlignment.Center;

        SolidBrush btmForeColor = new SolidBrush(Color.Green);
        SolidBrush btmBackColor = new SolidBrush(Color.Black);

        Font btmFont = new Font("Verdana", 90);
        SizeF textSize = new SizeF();
        textSize = g.MeasureString("Copyright", btmFont);

        float x = ((float)bitmap.Width - textSize.Width - 3);
        float y = ((float)bitmap.Height - textSize.Height - 3);
        float w = ((float)x + textSize.Width);
        float h = ((float)y + textSize.Height);

        RectangleF textArea = new RectangleF(x, y, w, h);
        g.FillRectangle(btmBackColor, textArea);

        g.DrawString("Copyright", btmFont, btmForeColor, textArea);
        btmForeColor.Dispose();
        btmBackColor.Dispose();
        btmFont.Dispose();
        g.Dispose();
        bitmap.Save(@"C:\Users\mike\Pictures\Panasonic\P1000016_0.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

this works for me, had to increase font size though.

Upvotes: 2

Related Questions