Resizing images in Silverlight using WriteableBitmap

代码
// create image source
Stream stream = file.OpenRead();
BitmapImage bmpImg 
= new BitmapImage();
bmpImg.SetSource(stream);
stream.Close();

// create temporary image from it
Image tmpImg = new Image();
tmpImg.Source 
= bmpImg;

// this is required by WriteableBitmap 
tmpImg.Measure(new Size(100100));
tmpImg.Arrange(
new Rect(00100100));

// prepare scaling to 100x100
ScaleTransform scaleTrans = new ScaleTransform();
double scale = (double)100 / (double)Math.Max(bmpImg.PixelHeight, bmpImg.PixelWidth);
scaleTrans.CenterX 
= 0;
scaleTrans.CenterY 
= 0;
scaleTrans.ScaleX 
= scale;
scaleTrans.ScaleY 
= scale;

// render
WriteableBitmap writeableBitmap = new WriteableBitmap(100100);
writeableBitmap.Render(tmpImg, scaleTrans);
writeableBitmap.Invalidate();

// final image
Image img = new Image();
img.Source 
= writeableBitmap;

 

posted @ 2010-09-02 14:40  大厨无盐煮  阅读(316)  评论(0编辑  收藏  举报