silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用WriteableBitmap调整Silverlight 3中的图像大小大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

这是我第一天使用Silverlight.我正在尝试原型化一个应用程序,其中(以及其他功能)应该能够调整用户提供的图像的大小.它应该能够一次处理和显示几个已调整大小的图像.我尝试过的最明显的方法似乎是“泄漏”内存,因为原始位图仍然以某种方式被引用,导致Silverlight在一段时间后分配数百兆字节的内存.我只是希望能够逐个加载图像,调整它们并保留小版本. 确切地说,我尝试过以下方法: >创建Sys
这是我第一天使用Silverlight.我正在尝试原型化一个应用程序,其中(以及其他功能)应该能够调整用户提供的图像的大小.它应该能够一次处理和显示几个已调整大小的图像.我尝试过的最明显的方法似乎是“泄漏”内存,因为原始位图仍然以某种方式被引用,导致Silverlight在一段时间后分配数百兆字节的内存.我只是希望能够逐个加载图像,调整它们并保留小版本.

确切地说,我尝试过以下方法

>创建System.Windows.Controls.Image的列表(并缩放它们).我并不感到惊讶,因为这没有用.
>创建由图像画笔填充的矩形列表.我也不感到惊讶.
>将位图呈现到System.Windows.Media.Imaging.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(100,100));
tmpImg.Arrange(new Rect(0,100,100));

// 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(100,100);
writeableBitmap.Render(tmpImg,scaleTrans);
writeableBitmap.Invalidate();

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

我希望我不会错过任何愚蠢的东西,但它看起来对我来说没问题并做正确的事(除了记忆问题).还请记住,代码质量不应该是生产质量;它只是一个快速而肮脏的原型.

我注意到我并不孤单;我在Silverlight中找到了与图像处理相关的问题.我也知道我可以使用一些第三方库,在服务器上进行处理或者甚至自己编写一些东西,但我很惊讶Silverlight不提供任何基本的图像处理功能.鉴于Silverlight的定位,它似乎并不是一个不寻常的要求.

解决方法

你看过 WriteableBitmapEx project吗?它是一个开源项目,为WriteableBitmap类提供了大量的扩展方法.以下是调整大小的方法

BitmapImage image = new BitmapImage();
image.Setsource(dialog.File.openRead());

WriteableBitmap bitmap = new WriteableBitmap(imagE);
WriteableBitmap resizedBitmap = bitmap.Resize(500,500,WriteableBitmapExtensions.Interpolation.bilinear);

// For uploading
byte[] data = resizedBitmap.ToByteArray();

大佬总结

以上是大佬教程为你收集整理的使用WriteableBitmap调整Silverlight 3中的图像大小全部内容,希望文章能够帮你解决使用WriteableBitmap调整Silverlight 3中的图像大小所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。