关于windowsphone载入超过2048大图的方法!

本文详细介绍了在Windows Phone 7.x环境下显示长图片的两种方法:使用浏览器显示,支持放大缩小、滑动流畅但不支持左右切换;另一种方法是通过分割图片拼接显示,感觉更正规但不能放大缩小且需要解码分割时间。同时提供了一段代码实现图片分割显示的功能。

众所周知,windowsphone7.0、7.1、8.0均无法载入超过2048像素的控件,所以要显示长图片就很费劲了。

目前有两个方法可以显示:

1、浏览器。 

 优点:支持放大缩小,滑动流畅等

缺点:有时候感觉不正规,不能左右切换图片。

2、分割图片拼接方式显示。

优点:感觉正规。

缺点:不能放大缩小了,且需要一点时间来进行解码分割。

说一下思路吧,载入图片的流然后从流里读取图片的高宽,如果大于2000的话则截取N多小图片然后添加到一个容器里。将会正确显示。

当然了,如果获取高宽直接设置的话也是可以显示出来的。可是会遇到一系列问题。如storyboard在操作一个超过2048的图片时会出现白边之类的。

有兴趣可以试一试

下面上码:

图片:http://ww4.sinaimg.cn/bmiddle/974f2750jw1duzsl9gq4dj.jpg 

长度:21539

首先载入图片

xaml

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ScrollViewer   x:Name="ImageScrollViewer">
                <StackPanel x:Name="ImageBox">
                    <Button Content="点我" Height="72" Name="button1" Width="205" Click="button1_Click" />
                </StackPanel>
            </ScrollViewer>
        </Grid>


  WebClient web = new WebClient();
            web.OpenReadCompleted += new OpenReadCompletedEventHandler(web_OpenReadCompleted);
            web.OpenReadAsync(new Uri("http://ww4.sinaimg.cn/bmiddle/974f2750jw1duzsl9gq4dj.jpg", UriKind.Absolute));


在回调里分割图片

        /// <summary>
        /// 图片最高为2000 如果超过2000则要进行分割。
        /// </summary>
        private const int KMaxImageHeight = 2000;
        void web_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            using (Stream ms = e.Result as Stream)
            {
                byte[] bytes = new byte[ms.Length];
                ms.Read(bytes, 0, bytes.Length);
                ms.Seek(0, SeekOrigin.Begin);

                byte[] bytesimg = new byte[2] { 0xFF, 0xC0 };
                byte[] byteImgW = new byte[2];//宽度
                byte[] byteImgH = new byte[2];//高度
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == bytesimg[0] && bytes[i + 1] == bytesimg[1])
                    {
                        byteImgH = new byte[2] { bytes[i + 5], bytes[i + 6] };
                        byteImgW = new byte[2] { bytes[i + 7], bytes[i + 8] };
                        break;
                    }
                }
                string strImgH = byteImgH[0].ToString("X2") + byteImgH[1].ToString("X2");
                string strImgW = byteImgW[0].ToString("X2") + byteImgW[1].ToString("X2");
                //图片的原始高度和宽度
                double BaseImageHeight = Convert.ToInt32(strImgH, 16);
                double BaseImageWidth = Convert.ToInt32(strImgW, 16);

                BaseImageHeight = BaseImageWidth > 480 ? BaseImageHeight * 480 / BaseImageWidth : BaseImageHeight;
                BaseImageWidth = BaseImageWidth > 480 ? 480 : BaseImageWidth;
                WriteableBitmap wb = new WriteableBitmap(Convert.ToInt32(BaseImageWidth), Convert.ToInt32(BaseImageHeight));
                wb.LoadJpeg(e.Result);

                Image ImgBase = new Image();
                ImgBase.Source = wb;
                ImgBase.Stretch = Stretch.None;

                if (BaseImageHeight > KMaxImageHeight)
                {
                    for (int i = KMaxImageHeight; i < BaseImageHeight + KMaxImageHeight; i += KMaxImageHeight)
                    {
                        int imageHeight = KMaxImageHeight;
                        if (i > BaseImageHeight)
                        {
                            imageHeight = Convert.ToInt32(BaseImageHeight) - KMaxImageHeight * (i / KMaxImageHeight - 1);
                        }
                        //图片变换。
                        TranslateTransform translate = new TranslateTransform();
                        translate.Y = -(i - KMaxImageHeight);
                        //bitmap
                        WriteableBitmap wbitmap = new WriteableBitmap(Convert.ToInt32(BaseImageWidth), imageHeight > KMaxImageHeight ? imageHeight - KMaxImageHeight : imageHeight);
                        wbitmap.Render(ImgBase, translate);
                        wbitmap.Invalidate();
                        //图片
                        Image image = new Image();
                        image.Source = wbitmap;
                        image.Width = wbitmap.PixelWidth;
                        image.Height = wbitmap.PixelHeight;
                        image.Margin = new Thickness(0, -1, 0, 0);
                        ImageBox.Children.Add(image);
                    }
                }
                else
                {
                    ImageBox.Children.Add(ImgBase);
                }
                bytes = null;
                WriteableBitmap bitmap = ImgBase.Source as WriteableBitmap;
                if (bitmap != null)
                {
                    bitmap = null;
                }
                ImgBase.Source = null;
                ImgBase.Visibility = Visibility.Collapsed;
                ImgBase = null;

                ms.Dispose();
            }
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值