• Windows Phone实用开发技巧(38):图片拼接


    图片拼接是拼图中一种,即将若干图片拼接成一张大图。本文将讲述如何在windows phone中实现图片的拼接。

    首先,我们准备一些拼图的原始图片,这些图片的长度、宽度各不一样,并且也不是等比例。

    private int width = 480;
    private int totalHeight = 0;
    string[] array = new string[] { "1.jpg", "2.jpg", "3.jpg", "4.jpg" };
    WriteableBitmap[] imagearray;
    int[] heightarray;

    一些常量的定义,array数组存储的是待拼接的图片列表

    imagearray存储的是图片对应的WriteableBitamp对象

    heightarray存储的是图片按比例缩放后的高度(宽度都为480)

    totalHeight表示图片拼接后的总高度

    我们在页面中方式一个ScrollViewer,然后将图片都放置在一个Canvas中,设置图片的距离顶部距离,最后将Canvas方式到ScrollViewer中:

    private void Join()
    {
        //images container
        Canvas canvas = new Canvas();
    
        //init array
        imagearray = new WriteableBitmap[array.Length];
        heightarray = new int[array.Length];
    
        for (int i = 0; i < array.Length; i++)
        {
            WriteableBitmap bitmap = FromContent(string.Format("Images/{0}", array[i]));
            double wr = width / (double)bitmap.PixelWidth;
            int height = (int)(bitmap.PixelHeight * wr);
            Image img = new Image() { Source = bitmap, Stretch = Stretch.Fill, Width = width, Height = height };
            Canvas.SetLeft(img, 0);
            Canvas.SetTop(img, totalHeight);
            canvas.Children.Add(img);
            totalHeight += height;
    
            imagearray[i] = bitmap;
            heightarray[i] = height;
        }
        canvas.Height = totalHeight;
        scrollviewer.Content = canvas;
    }

    其中需要注意的就是将图片按比例缩放,为了防止保存图片时候重新计算一遍高度,我们将高度和图片保存到数据中。

    下面来看一下保存的方法,我们需要得到一个ScrollViewer的WriteableBitmap,那么能不能使用直接对UI元素的截屏呢?答案是否定的,因为截屏我们只能得到屏幕大小的图片,不能得到整个长图。

    那么我们应该怎么做呢,其实思路跟上面的展示方式一样,我们将图片拼接起来,具体的代码如下:

    //遍历,将每张图copy至大图的相应位置
    WriteableBitmap output = new WriteableBitmap(width, totalHeight);
    int toTop = 0;
    for (int i = 0; i < imagearray.Length; i++)
    {
        var wb = imagearray[i].Resize(width, heightarray[i], WriteableBitmapExtensions.Interpolation.NearestNeighbor);
        Rect dest = new Rect(0, toTop, width, heightarray[i]);
        Rect source = new Rect(0, 0, width, heightarray[i]);
        output.Blit(dest, wb, source);
        toTop += heightarray[i];
    }
    
    SaveImage(output);

    遍历图片,将图片copy至一张大图的某些部分

    保存方法是将图片保存到相册中,当然我们也可以保存到独立存储空间中:

    private void SaveImage(WriteableBitmap bit)
    {
        string msg = "";
        try
        {
            byte[] imageBuffer;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                bit.SaveJpeg(memoryStream, bit.PixelWidth, bit.PixelHeight, 0, 100);
                imageBuffer = memoryStream.ToArray();
            }
            using (MediaLibrary library = new MediaLibrary())
            {
                library.SavePicture(string.Format("{0}.jpg", DateTime.Now.ToFileTime().ToString()), imageBuffer);
            }
    
            msg = "保存成功";
        }
        catch (Exception ex)
        {
            msg = "保存失败";
        }
        finally
        {
            MessageBox.Show(msg);
        }
    }

    源代码你可以在这里找到.

  • 相关阅读:
    nvm安装node的问题
    前端必读:浏览器内部工作原理
    读书笔记:《高性能网站建设指南》
    学习前端我读过的书
    Canvas绘制圆形进度条
    gitlab升级方法
    设置root远程连接ubuntu上mysql
    SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
    JSTL 格式化输出 Calendar
    在Maven的配置文件中,自定义私有仓库地址和设置下载的jar包的保存位置
  • 原文地址:https://www.cnblogs.com/alexis/p/windowsphone_put_images_together.html
Copyright © 2020-2023  润新知