storing and restoring graphics RRS feed

  • Question



    i want to draw a bitmap programaticaly and store it to a db.when i try to display it from the db the drawings appear in a black back ground . how can i solve this problem.

    i restore the bitmap by using a dataset and data bind contrls from

    visual studio designer and codes to draw and store the bitmap is here :


    private void button1_Click(object sender, EventArgs e)


    pictureBox1.Paint+=new PaintEventHandler(pictureBox1_Paint);



    private void pictureBox1_Paint(object sender, PaintEventArgs e)




    private void PaintImage(Graphics gr)


    int p1 = Convert.ToInt32(textBox1.Text);

    int p2 = Convert.ToInt32(textBox2.Text);

    int p3 = Convert.ToInt32(textBox3.Text);

    Pen myPen = new Pen(Color.Blue);

    gr.DrawCurve(myPen, new Point[] { new Point(50, p1), new Point(100, p2), new Point(150, p3) });


    private void button2_Click(object sender, EventArgs e)


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

    Graphics gr = Graphics.FromImage(bmp);

    PaintImage(gr );

    MemoryStream stream = new MemoryStream();

    bmp.Save (stream, System.Drawing.Imaging.ImageFormat.Bmp );

    byte[] bytes = stream.ToArray();

    SqlConnection sqc = new SqlConnection("server=farhad;database=graphics_test;integrated security=sspi");

    SqlCommand cmd = new SqlCommand("insert graph values(@p1,@p2,@p3,@graph)");

    cmd.Connection = sqc;

    cmd.Parameters.Add("@p1", SqlDbType.NVarChar);

    cmd.Parameters.Add("@p2", SqlDbType.NVarChar);

    cmd.Parameters.Add("@p3", SqlDbType.NVarChar);

    cmd.Parameters.Add("@graph", SqlDbType.Image);

    cmd.Parameters[0].Value = textBox1.Text;

    cmd.Parameters[1].Value = textBox2.Text;

    cmd.Parameters[2].Value = textBox3.Text;

    cmd.Parameters[3].Value = bytes;


    SqlDataReader reader = cmd.ExecuteReader();





    Friday, April 20, 2007 9:41 AM

All replies

  • Hi sepehrsam,


       So this is a pixmap, which makes it all the more difficult to get a uniformly black background as a bug.  In a bitmap, each pixel is represented by 0 or 1, which generates a black and white image.  A pixmap is made up of RGB components packed into an int or 3 floats or whatever representation you choose to use.  The upshot is that black is 0 and 0 is a common default for things like integers and such.  One other thing to note is that if you intend to draw anything to a drawable surface, you have to clear it first.  So do gr.clear( <e.g. Color.white> ) before your draw code to ensure that you are painting over the background you want.


    Hope this helps!


    John (MSFT)

    Friday, April 27, 2007 9:55 PM