Search code examples
c#bitmappictureboxsystem.drawingdrawing2d

C# Bitmap To PictureBox is not working well


I'm trying to make some gradient & preview it in a pictureBox without saving but it seems not working:

        if (isChanged == true)
        {
                re = new Rectangle(0, 0, int.Parse(textBox5.Text), int.Parse(textBox4.Text));
                currectBrush = new System.Drawing.Drawing2D.LinearGradientBrush(re, System.Drawing.ColorTranslator.FromHtml("#FC00FF"), System.Drawing.ColorTranslator.FromHtml("#00DBDE"), -45f); ;
                bitmap = new Bitmap(int.Parse(textBox5.Text), int.Parse(textBox4.Text));
                using (bitmap)
                using (var graphics = Graphics.FromImage(bitmap))
                {
                    currectBrush = new System.Drawing.Drawing2D.LinearGradientBrush(re, System.Drawing.ColorTranslator.FromHtml(textBox1.Text), System.Drawing.ColorTranslator.FromHtml(textBox2.Text), int.Parse(textBox3.Text));

                    graphics.FillRectangle(currectBrush, re);
                    pictureBox1.Image = bitmap;

                }
                isChanged = false;
        }

And this is what pictureBox looks like after running this part of code:


Solution

  • Remove this line because it disposes the resources

    using (bitmap)