生成缩略图

public bool GetReducedImage(string srouceImage, int smallHeight, ref string retInfo)
{
Image ReducedImage = null;
Image ResourceImage=null;
retInfo = "";
try
{
string smallfilename = Path.GetFileNameWithoutExtension(srouceImage) + "_small" + Path.GetExtension(srouceImage);

string targetFilePath = Path.GetDirectoryName(srouceImage) + Path.DirectorySeparatorChar + smallfilename;

if (File.Exists(targetFilePath))
{
File.Delete(targetFilePath);
}

ResourceImage = Image.FromFile(srouceImage);

int ImageHeight = smallHeight;

int ImageWidth = Convert.ToInt32((float)smallHeight / ResourceImage.Height * ResourceImage.Width);

Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);

ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);

return true;
}
catch (Exception ex)
{
retInfo = ex.Message;
return false;
}
finally
{
if (ReducedImage != null)
{
ReducedImage.Dispose();
}
if (ResourceImage != null)
{
ResourceImage.Dispose();
}
}
}

public bool ThumbnailCallback()
{
return false;
}

posted @ 2017-03-19 11:34  Mr▪King  阅读(145)  评论(0编辑  收藏  举报