Home > DeveloperSection > Forums > how to save a shape as bmp file
Madhu Mitha
Madhu Mitha

Total Post:31

Posted on    March-27-2015 3:24 AM

 .NET VB.Net 

 1 Reply(s)
 1065  View(s)
Rate this:
hi thr...

       I want to save three shape[rectangle] as a single bmp file.. is there option to save it..
my code is

Public Class Form1

    Dim bmp As New Bitmap(Me.Width, Me.Height)
    Private Sub PaintTheForm()
        Dim bmp As New Bitmap(Me.Width, Me.Height)
        Using g As Graphics = Graphics.FromImage(bmp)
            Dim blackPen As New Pen(Color.Black, 1)
            'to draw first rectangle rectangle
            Dim rect As New Rectangle(0, 0, 30, 16)
            g.DrawRectangle(Pens.Black, rect)
            'to draw second rectangle
            Dim rect1 As New Rectangle(30, 0, 114, 8)
            g.DrawRectangle(Pens.Black, rect1)
            'to draw thrid rectangle
            Dim rect2 As New Rectangle(30, 8, 114, 8)
            g.DrawRectangle(Pens.Black, rect2)
            ' To type text
            Dim big_font As New Font("Comic Sans MS", 12, FontStyle.Bold, GraphicsUnit.Pixel)
            g.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixel
            g.DrawString("helo", big_font, Brushes.Black, 0, 0)
            ' End Sub
            Me.BackgroundImage = bmp

        End Using
    End Sub

    Private Sub btnPaint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaint.Click
    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

        SaveFileDialog1.FileName = ""
        If SaveFileDialog1.ShowDialog <> 1 Then Exit Sub
            Select Case SaveFileDialog1.FilterIndex
                Case 1
                    bmp.Save(SaveFileDialog1.FileName, Imaging.ImageFormat.Bmp)
                Case 2
            End Select
            MsgBox("Can not save the LED image!", 48, "LED Saving")
        End Try
    End Sub

End Class

when I tried to save this I could not save the three rectangle box... so can anyone help in solving my problem


Kamlakar Singh
Kamlakar Singh

Total Post:194

Posted on    March-30-2015 7:27 AM

Hi madhu

Try this code:

If you want to save the image that is displayed in the picture box, complete with any lines that may have been drawn on top of it during run-time, you can use the Control.DrawToBitmap method

Here's an example of a complete solution. First, the user is prompted by a save dialog (entitled "Save Image" and filtering to bitmap images (*.bmp) by default). If they click OK, the image displayed in the picture box is drawn to a temporary bitmap, and that temporary bitmap is saved to the location they specified. If they click Cancel, the file is not saved and the method simply exits.

private void Save_Click(object sender, EventArgs e)


    //Show a save dialog to allow the user to specify where to save the image file

    using (SaveFileDialog dlgSave = new SaveFileDialog())


        dlgSave.Title = "Save Image";

        dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*";

        if (dlgSave.ShowDialog(this) == DialogResult.OK)


            //If user clicked OK, then save the image into the specified file

            using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))


                picturebox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));






Don't want to miss updates? Please click the below button!

Follow MindStick