日期:2014-05-18  浏览次数:20836 次

如何将System.Windows.Controls.Image 的对象转换为Stream 的对象
如题

我用silverlight做了一个照片上传的工具,想在上传的时候设置照片的最大尺寸,请指教

------解决方案--------------------
///<summary> 
/// 生成缩略图 
/// </summary> 
/// <param name="width">缩略图宽度</param> 
/// <param name="height">缩略图高度</param>
public static Image MakeThubnail(Image image, int width,int height)
{
int desWidth=image.Width;
int desHeight=image.Height;

MemoryStream ms = new MemoryStream();

if (image.Width > width) {
desWidth = width;
desHeight = Convert.ToInt32((image.Height * width) / image.Width);
}

if (desHeight > height) {
desWidth = Convert.ToInt32((desWidth * height) / desHeight);
desHeight = height;
}

Console.WriteLine(desHeight.ToString());
int x = 0;
int y = 0;

int ow = image.Width;
int oh = image.Height;

Image bitmap = new Bitmap(desWidth, desHeight);

Graphics g = Graphics.FromImage(bitmap);

g.InterpolationMode = InterpolationMode.High; //设置高质量插值法 

g.SmoothingMode = SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度 

g.Clear(Color.Transparent); //清空画布并以透明背景色填充

g.DrawImage(image, new Rectangle(0, 0, desWidth, desHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);

bitmap.Save(ms, ImageFormat.Jpeg);

image = Image.FromStream(ms);

return image;

}
最近做项目,自己写的一个方法