troubleshooting Question

Retrive Taransparent PNG from Database

Avatar of MCofer
MCofer asked on
Microsoft SQL ServerImages and PhotosMultimedia Programming
1 Comment1 Solution329 ViewsLast Modified:
I am creating Png thumbnails with transparency and saving them to a SQL2008 database
When I retrieve them and display on an aspx web page the background is black.
The priginal thumb written to a file display correctly;

Saving to file 
image = new Bitmap(photoSpec);
            photo.Thumb = (Bitmap)GetThumbnail(image, 140);
            if (!Directory.Exists(thumbDir))
                Directory.CreateDirectory(thumbDir);
            photo.Thumb.Save(thumbDir + photo.PhotoId + ".PNG", ImageFormat.Png);
 
 
Writing To DB
ms = new MemoryStream();
_thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] thumbBytes = ms.GetBuffer();
_thumb.Dispose();
ms.Close();
sqlString = new StringBuilder();
sqlString.AppendFormat("INSERT INTO Images(ImageId,  Thumb)");
sqlString.AppendFormat(" VALUES(@ImageId,  @Thumb) ");
using (SqlCommand sqlCommand = new SqlCommand(sqlString.ToString(), sqlConn))
{
    sqlCommand.Parameters.AddWithValue("@ImageId", _photoId);
    sqlCommand.Parameters.AddWithValue("@Thumb", thumbBytes);
    sqlCommand.ExecuteScalar();
}
 
Reading from DB
string sqlString = "SELECT Thumb FROM Images WHERE ImageId = @IMAGE_ID";
            using (SqlCommand sqlCommand = new SqlCommand(sqlString.ToString(), sqlConn))
            {
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.Parameters.AddWithValue("@IMAGE_ID", theID);
                object thumb = (byte[])sqlCommand.ExecuteScalar();      
            }
 
 
Displaying
  MemoryStream ms = new MemoryStream(thumb);
 Image img = Image.FromStream(ms);
 using (MemoryStream stmMemory = new MemoryStream())
{
            context.Response.Clear();
            context.Response.ContentType = "image/png";
            img.Save(stmMemory, System.Drawing.Imaging.ImageFormat.Png);
            stmMemory.WriteTo(context.Response.OutputStream);
            context.Response.End();
        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros