• WriteableBitmap 巧学巧用


    WriteableBitmap我想大家并不陌生吧,它是一个基于内存的图像管理类,大家可以把它认为图像是一堆存储在内存中的数据,这些数据可由WriteableBitmap管理和分配。
    这里我就给大家讲一些关于WriteableBitmap的一些使用技巧:
    实现自绘
    众所周知,目前为止,微软还没有开放自绘接口,如果你真的想在界面上自已绘制一个字符串,都有些困难呢。下面的代码正是使用WriteableBitmap来实现自绘的方案
    private void RenderString(WriteableBitmap bitmap, string stringToRender)
    {
    TextBlock textBlock = new TextBlock();
    textBlock.Text = stringToRender;
    //设置 font, size,等等
    bitmap.Render(textBlock, null);
    bitmap.Invalidate();
    }
    怎么样,很简单吧,他是通过将TextBlock中的文本绘制到WriteableBitmap来实现的,在这里我发挥一下,那不就可以通过这个方法,来实现一个图片水印的功能么,赶快去试试吧
    顺便说一点,这里我要介绍一个更强大的开源的库writeablebitmapex,如果大家想要绘制更复杂的的图像如:点,线,曲线,阴影,形状,以及实现一些常用的图像数据处理功能,那么这个库将是大家
    最好的选择。
    图像的缩放存储
    如果你想将一张图片改变大小,那么你可以用以下的方法去实现
    WriteableBitmap resizedImage = new WriteableBitmap(imageToResize);//imageToResize is BitmapImage
    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
    using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
    {
    double maxHeight = newWidth;
    double maxWidth = newHeight;
    double scaleX = 1;
    double scaleY = 1;
    if (pixHt > maxHeight)
    scaleY = maxHeight / pixHt;
    if (pixWt > maxWidth)
    scaleX = maxWidth / pixWt;


    double scale = Math.Min(scaleY, scaleX);
    int newWidth1 = Convert.ToInt32(pixWt * scale);
    int newHeight1 = Convert.ToInt32(pixHt * scale);

    resizedImage.SaveJpeg(isfs, newWidth1, newHeight1, 0, 70);
    isfs.Close();
    }
    }

    在这里,imageToResize就是你输入的图像,你可以将它存储为目标大小的图像文件
    对控件(全屏)进行截图
    在很多应用中,如果要对当前页面进行截图,应该怎么办呢,这时WriteableBitmap就能帮助到你了。
    把页面截图,并保存到内存
    WriteableBitmap wb = new WriteableBitmap(UiRoot, null);//将UI页面的根元素传入,可将当面页面的截图保存到WriteableBitmap 
    MemoryStream ms = new MemoryStream();

    wb.SaveJpeg(ms, myWidth, myHeight, 0, 100);//保存到内存MemoryStream
    
    
    BitmapImage bmp = newBitmapImage(); //把截图转化为BitmapImage 
    bmp.SetSource(ms);
    
    
    using (var isoFileStream =newIsolatedStorageFileStream("myPicture.jpg",FileMode.OpenOrCreate,IsolatedStorageFile.GetUserStoreForApplication())) 
    {                    
        wb
    .SaveJpeg(isoFileStream, myWidth, myHeight,0,100);  //把截图存储到独立存储                  
    }
    将存储在Sql数据库的图片二进制数据载入到内存
    有些时候图片数据是以二进制数据保存到sqlite数据库中的,下面将是,如何把这些二进制数据还原成图像格式
    public static byte[] ConvertToBytes(String imageLocation)
    {
    StreamResourceInfo sri = Application.GetResourceStream(new Uri(imageLocation, UriKind.RelativeOrAbsolute));
    BinaryReader binary = new BinaryReader(sri.Stream);

    byte[] imgByteArray = binary.ReadBytes((int)(sri.Stream.Length));

    binary.Close();
    binary.Dispose();
    return imgByteArray;
    }

    public static WriteableBitmap ConvertToImage(Byte[] inputBytes)
    {
    MemoryStream ms = new MemoryStream(inputBytes);
    WriteableBitmap img = new WriteableBitmap(400, 400);

    img.LoadJpeg(ms);

    return (img);
    }

    我希望你能喜欢我的文章!如果你有更多想法,请到
    卤面网 wp7开发论坛(codewp7.com)问答区联系我,我会很高兴知道你在想什么。同时wp7交流QQ群172765887中,也能找到我的身影,感谢大家,也欢迎大家关注我的微薄(www.weibo.com/codewp7)

    
    
    
    
  • 相关阅读:
    截屏 多难未遂
    捕捉异常
    Android中缓存记忆
    Android中的线程池
    悄悄为Android中解决部分适配问题哦!
    java中的服务端跳转与客户端跳转区别与联系
    doget(),doput()方法的使用
    基本概念--同步,异步
    java--SimpleDataFormat类
    java--9
  • 原文地址:https://www.cnblogs.com/sonyye/p/2389193.html
Copyright © 2020-2023  润新知