locked
Getting a black image when creating thumbnail RRS feed

  • Question

  • User-587364632 posted

    I'm trying to save two images to the DB, the original image and a thumbnail of the original.

    But all I'm getting is a black image :/
    I'm also trying to keep the proportions of the original image when creating the thumbnail, and I hope I'm right.

    System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    decimal w = img.Width, h = img.Height;
    int newHeight = 0;
    
    // Keep proportions of thumbnail if width is above 120px
    if (img.Width > 120) newHeight = (int)((w / h) * 120); else newHeight = img.Height;
    
    System.Drawing.Image thumb = new System.Drawing.Bitmap(120, newHeight);
    MemoryStream ms = new MemoryStream();
    thumb.Save(ms, img.RawFormat);
    Byte[] bytesThumb = ms.ToArray();
    
    FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                            
    sqlParam = cmd.Parameters.Add("@thumb", SqlDbType.VarBinary, -1);
    sqlParam.Value = bytesThumb;
    
    sqlParam = cmd.Parameters.Add("@data", SqlDbType.VarBinary, -1);
    sqlParam.Value = bytes;

    Thanks in advance!

    /zalk

    Thursday, October 28, 2010 5:27 AM

Answers

  • User-587364632 posted

    Got it:

    System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    decimal w = img.Width, h = img.Height;
    int newHeight = 0, width = 120;
    // Keep proportions of thumbnail if width is above 120px
    if (img.Width > width) newHeight = (int)((h / w) * width); else newHeight = img.Height;
    Bitmap bmp = new Bitmap(120, newHeight);
    var thumb = Graphics.FromImage(bmp);
    thumb.CompositingQuality = CompositingQuality.HighQuality;
    thumb.SmoothingMode = SmoothingMode.HighQuality;
    thumb.InterpolationMode = InterpolationMode.HighQualityBicubic;
    var rec = new Rectangle(0, 0, width, newHeight);
    thumb.DrawImage(img, rec);
    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    Byte[] bytesThumb = ms.ToArray();
    
    FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    
    sqlParam = cmd.Parameters.Add("@data", SqlDbType.VarBinary, -1);
    sqlParam.Value = bytes;
    
    sqlParam = cmd.Parameters.Add("@fileType", SqlDbType.NVarChar, 50);
    sqlParam.Value = cont;
    
    sqlParam = cmd.Parameters.Add("@thumb", SqlDbType.VarBinary, -1);
    sqlParam.Value = bytesThumb;
    
    sqlParam = cmd.Parameters.Add("@thumbType", SqlDbType.NVarChar, 50);
    sqlParam.Value = "image/png";


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 28, 2010 8:58 AM

All replies

  • User-1254135497 posted

    hi.....

    try dis:

    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
                    {
                        string filename1;
                        string ext;
                        filename1 = FileUpload1.PostedFile.FileName;
                        int contentLength = FileUpload1.PostedFile.ContentLength;
                        ext = filename1.Substring(filename1.LastIndexOf("."));
                        if (ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".JPG" || ext == ".PNG" || ext == ".GIF" || ext == ".JPEG" || ext == ".jpeg")
                        {
                            const int ThumbnailWidth = 150;
                            const int ThumbnailHeight = 150;


                            byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];


                            HttpPostedFile Image = FileUpload1.PostedFile;

                            Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
                            string filePath = Server.MapPath("uploaded_images/" + filename1);
                            FileUpload1.PostedFile.SaveAs(filePath);
                            string ImagePath = @"uploaded_images/" + filename1;
                            objESchool.ImagePath = ImagePath;
                            objESchool.FileContent = myimage;

                            //Creating Thumbnail
                            System.Drawing.Image image = System.Drawing.Image.FromStream(Image.InputStream);
                            Bitmap bitmap = new Bitmap(image, ThumbnailWidth, ThumbnailHeight);
                            {
                                bitmap.Save(MapPath("uploaded_images/t__" + Image), image.RawFormat);
                                FileStream fs = new FileStream(MapPath("uploaded_images/t__" + Image), FileMode.Open, FileAccess.Read);
                                byte[] picbyte = new byte[fs.Length];
                                fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                                objESchool.ThumbFileContent = picbyte;

                                Page.RegisterStartupScript("Samatha", "<span style=\"color:green;\">Thumbnail " + " created...</span><br><br>");
                            }

                        }
                        else
                        {
                            Response.Write(String.Format("<span style=\"color:red;\">Image size " +
                               "exceeding limits: {0} KB</span><br><br>", contentLength / 1024));
                        }
                    }
                    else
                    {
                        Page.RegisterStartupScript("Samatha", "<Script>alert('Only gif,png or jpg format files supported.')</Script>");
                        return;

                    }

    Mark as answer if it is helped....

    Thursday, October 28, 2010 6:53 AM
  • User-587364632 posted

    I have looked through almost 20 different code examples, and I still can't figure out why I get a black thumbnail.

    I'm sorry, but I'm not getting any wiser here either. I seem to be doing everything right :/

    Thursday, October 28, 2010 7:35 AM
  • User-587364632 posted

    Got it:

    System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    decimal w = img.Width, h = img.Height;
    int newHeight = 0, width = 120;
    // Keep proportions of thumbnail if width is above 120px
    if (img.Width > width) newHeight = (int)((h / w) * width); else newHeight = img.Height;
    Bitmap bmp = new Bitmap(120, newHeight);
    var thumb = Graphics.FromImage(bmp);
    thumb.CompositingQuality = CompositingQuality.HighQuality;
    thumb.SmoothingMode = SmoothingMode.HighQuality;
    thumb.InterpolationMode = InterpolationMode.HighQualityBicubic;
    var rec = new Rectangle(0, 0, width, newHeight);
    thumb.DrawImage(img, rec);
    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    Byte[] bytesThumb = ms.ToArray();
    
    FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    
    sqlParam = cmd.Parameters.Add("@data", SqlDbType.VarBinary, -1);
    sqlParam.Value = bytes;
    
    sqlParam = cmd.Parameters.Add("@fileType", SqlDbType.NVarChar, 50);
    sqlParam.Value = cont;
    
    sqlParam = cmd.Parameters.Add("@thumb", SqlDbType.VarBinary, -1);
    sqlParam.Value = bytesThumb;
    
    sqlParam = cmd.Parameters.Add("@thumbType", SqlDbType.NVarChar, 50);
    sqlParam.Value = "image/png";


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 28, 2010 8:58 AM