troubleshooting Question

Retrive Taransparent PNG from Database

Avatar of MCofer
MCofer asked on
Microsoft SQL ServerMultimedia ProgrammingImages and Photos
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();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Member_2_4694817

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 1 Comment.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 1 Comment.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004