WriteableBitmap 巧学巧用
发布时间:2020-12-14 04:37:56 所属栏目:大数据 来源:网络整理
导读:分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net WriteableBitmap管理和分配。 WriteableBitmap我想大家并不陌生吧,它是一个基于内存的图像管理类,大家可
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net 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 BitmapImageusing (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,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) 再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |