Value can not be NULL when you insert an image

0

When I try to insert an image to the MySQL database everything works normal, but when I run a function that makes the size of my image smaller and try to insert it, it shows me an error that says

this is the code to select the image:

private void pbImagen_DoubleClick(object sender, EventArgs e)
    {


        try {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "choose Image(*.JPG;*.PNG)|*.jpg;*.png";

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                convertir conv = new convertir()

                    pbImagen.Image = conv.RedimensionarImage(Bitmap.FromFile(ofd.FileName));
                    pbImagen.SizeMode = PictureBoxSizeMode.CenterImage;
                    pbImagen.SizeMode = PictureBoxSizeMode.StretchImage;
            }


          }
           catch {
                   MessageBox.Show("No Ingresaste Ninguna Imagen");     
                 }



    }

then we have the code that reduces the size of the image:

                public Bitmap RedimensionarImage(Image imgO)
        {
            var redimencion = new Bitmap(100, 100);
            Graphics.FromImage(redimencion).DrawImage(imgO, 0, 0, 100, 100);
            Bitmap ImagenFinal = new Bitmap(redimencion);

            return ImagenFinal;
        }

and then I try to insert it into the database but it tells me that the value is null. if this I try without reducing the image if it works. someone to help me please

    
asked by jeison 26.07.2018 в 22:04
source

1 answer

0

Friend try this, apparently your method in charge of converting to byte [] is bursting:

       try
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "choose Image(*.JPG;*.PNG)|*.jpg;*.png";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Bitmap newBitmap = new Bitmap(100, 100);
                //Image Image = RedimensionarImage(Bitmap.FromFile(ofd.FileName));
                newBitmap = RedimensionarImage(Bitmap.FromFile(ofd.FileName));
                MemoryStream NewMemoryStream = new MemoryStream();
                newBitmap.Save(NewMemoryStream, ImageFormat.Png);
                byte[] ReturnedThumbnail;
                ReturnedThumbnail = NewMemoryStream.ToArray();//en este punto ya te quedo el arreglo
                //pbImagen.SizeMode = PictureBoxSizeMode.CenterImage;
                //pbImagen.SizeMode = PictureBoxSizeMode.StretchImage;
            }


        }
        catch(Exception ex)
        {
            MessageBox.Show("No Ingresaste Ninguna Imagen");
        }
    }

    public static Bitmap RedimensionarImage(Image image)
    {
        Bitmap resizedImage = new Bitmap(100, 100);
        using (Graphics gfx = Graphics.FromImage(resizedImage))
        {
            gfx.DrawImage(image, new Rectangle(0, 0, 100, 100),
                new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
        }
        return resizedImage;
    }
}

I adjust some things to the code to be able to run it on my machine. Well I left a few brackets but you're adjusting, this I did based on this post:

Resize image c #

I hope it serves you!

    
answered by 27.07.2018 в 21:53