Hi,
Narshe wrote:
I'm creating a thumbnail from an image saved in a db. If I write the
data directly to the stream, the image shows up perfectly.
Response.Clear();
Response.ContentType = myImage.ContentType;
Response.BinaryWrite( myImage.File );
But if i try and make a thumb, the image is distorted.
MemoryStream stream = new MemoryStream( myImage.File );
Bitmap bitmap = new Bitmap( stream );
Image image = bitmap.GetThumbnailImage( 100, 100, null, IntPtr.Zero );
image.Save( Response.OutputStream, bitmap.RawFormat );
Is there a way to make this clear?
I think that what you mean with "distorted" is that you create
thumbnails having a size of 100*100 pixels, regardless of the original
size. If you want to keep the original proportions, you must calculate
the corresponding width, respectively height:
Bitmap bmpOriginal = new Bitmap( strPath );
int iNewHeight = m_iMaxSize;
float fRate = (float) iNewHeight / (float) bmpOriginal.Height;
int iNewWidth = (int) Math.Round( (float) bmpOriginal.Width * fRate );
if ( iNewWidth m_iMaxSize )
{
iNewWidth = m_iMaxSize;
fRate = (float) iNewWidth / (float) bmpOriginal.Width;
iNewHeight = (int) Math.Round( (float) bmpOriginal.Height * fRate );
}
Bitmap bmpNew = new Bitmap( bmpOriginal, iNewWidth, iNewHeight );
bmpNew.Save( Response.OutputStream, bmpOriginal.RawFormat );
In the code above, I have m_iMaxSize set by the user (in your case 100).
I use this way (more or less) to create thumbnails dynamically in this page:
http://www.galasoft-lb.ch/pictures/welcome.aspx
It's true that the thumbnails look less clear than if produced using a
graphics application (I use SuperJpg when I want to make "static"
thumbnails). However, it's not that bad I think.
HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering:
http://www.galasoft-LB.ch
PhotoAlbum:
http://www.galasoft-LB.ch/pictures
Support children in Calcutta:
http://www.calcutta-espoir.ch