开发者

Silverlight 4 WriteableBitmap ScaleTransform Exception but was working in v3

I am getting the following exception for code that used to work in silverlight 3 but has stopped working since upgrading to silverlight 4:

System.AccessViolationException was unhandled Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var OpenFileDialog = new OpenFileDialog();
            OpenFileDialog.Filter = "*.jpg|*.jpg";

   开发者_StackOverflow中文版         if (OpenFileDialog.ShowDialog() == true)
            {
                var file = OpenFileDialog.Files.ToArray()[0];
                ScaleStreamAsBitmap(file.OpenRead(), 200);
            }
        }

        public static WriteableBitmap ScaleStreamAsBitmap(Stream file, int maxEdgeLength)
        {
            file.Position = 0;
            var src = new BitmapImage();
            var uiElement = new System.Windows.Controls.Image();
            WriteableBitmap b = null;
            var t = new ScaleTransform();

            src.SetSource(file);
            uiElement.Source = src;

            //force render
            uiElement.Effect = new DropShadowEffect() { ShadowDepth = 0, BlurRadius = 0 }; ;

            //calc scale
            double scaleX = 1;
            double scaleY = 1;
            if (src.PixelWidth > maxEdgeLength)
                scaleX = ((double)maxEdgeLength) / src.PixelWidth;
            if (src.PixelHeight > maxEdgeLength)
                scaleY = ((double)maxEdgeLength) / src.PixelHeight;
            double scale = Math.Min(scaleX, scaleY);
            t.ScaleX = scale;
            t.ScaleY = scale;

            b = new WriteableBitmap(uiElement, t);

            return b;
        }
    }
}

Thanks


I had the same problem and I succeeded to resolve it!

b=new new WriteableBitmap(0, 0);
b.SetSource(file);
b.Render( new Image() { Source = src, Effect = new DropShadowEffect() { ShadowDepth = 0, BlurRadius = 0 } }, new ScaleTransform() { ScaleX = scaleX , ScaleY = scaleY });

And you can remove :uiElement and file.Position!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜