抱歉标题不好
我有这个功能:
public Stream PictureStream( int size = 0 )
{
if( size == 0 )
{
return new MemoryStream( PictureFile );
}
else
{
ImageFormat format = null;
if( FileFormat == ".jpg" || FileFormat == ".jpeg" )
{
format = ImageFormat.Jpeg;
}
else if( FileFormat == ".png" )
{
format = ImageFormat.Png;
}
else
{
format = ImageFormat.Png;
}
var ms = new MemoryStream( PictureFile );
var returnImage = Image.FromStream( ms );
var img = returnImage.Resize( size );
img.Save( ms , format );
return ms;
}
}
这个ActionResult:
public FileStreamResult Image( long userkey , int? size )
{
var image = UserImagesService.Find( userkey );
image.Size = size == null ? 0 : ( int )size;
return new FileStreamResult( image.Stream , image.ContentType );
}
当我的大小为空时,图像加载正常,但是当我在 Firefox 中传递大小时,我看到了这个错误:
The image “http://localhost:8609/Profiles/Image/4006242570097652224/150” cannot be displayed because it contains errors.
它很好地保存在磁盘上
我的问题是什么?
更新
我的调整大小功能:
public static Image Resize( this Image imgToResize , int size )
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ( ( float )size / ( float )sourceWidth );
nPercentH = ( ( float )size / ( float )sourceHeight );
if( nPercentH < nPercentW )
{
nPercent = nPercentH;
}
else
{
nPercent = nPercentW;
}
int destWidth = ( int )( sourceWidth * nPercent );
int destHeight = ( int )( sourceHeight * nPercent );
Bitmap b = new Bitmap( destWidth , destHeight );
Graphics g = Graphics.FromImage( ( System.Drawing.Image )b );
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage( imgToResize , 0 , 0 , destWidth , destHeight );
g.Dispose();
return ( System.Drawing.Image )b;
}