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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值