WriteableBitmapEx库是WriteableBitmap扩展方法的集合。该库增加了基本(绘图)功能。
源代码现在位于GitHub上: https : //github.com/teichgraf/WriteableBitmapEx/ 描述该WriteableBitmapEx库的扩展方法的集合 WriteableBitmap的。WriteableBitmap类可用于所有XAML版本,包括Windows Phone,WPF,WinRT Windows Store XAML,(Windows 10)UWP和Silverlight。它允许直接操作位图,并可用于图像操作,通过直接绘制到位图等来生成快速的过程图像。 WriteableBitmap API非常简单,只有原始的 Pixels数组可用于此类操作。WriteableBitmapEx库尝试使用易于使用的扩展方法(如内置方法)来弥补这一点,并提供 GDI +喜欢的功能。该库扩展了WriteableBitmap类,使其具有基本和快速(2D绘图)功能,转换方法和功能,以结合(拆分)WriteableBitmap。 使用部分类方法将扩展方法分组到不同的C#文件中。通过直接使用特定的源代码文件或通过内置的二进制文件使用全部功能,可以仅包括几种方法。 最新的二进制文件始终以 NuGet软件包的形式提供。 WriteableBitmapEx也已移植到Windows Embedded。 有关将来要添加的功能的列表,请参阅问题跟踪器。请使用 GitHub Issues功能 添加尚未报告的新问题。
GDI +类似WriteableBitmap的绘图功能。 支持Windows Phone Silverlight,Windows Phone WinRT,桌面Silverlight,WPF,Windows 8 / 8.1 WinRT XAML和Windows 10 UWP。
- 基础
- 支持 Color结构(将执行alpha预乘)
- 还可以重载以更快地将int32作为颜色(假定已经是alpha预乘)
- 具有各种重载的SetPixel方法
- GetPixel方法获取指定x,y坐标处的像素颜色
- 快速清除方法
- 快速复制方法以复制WriteableBitmap
- 将给定函数应用于位图的所有像素的ForEach方法
- 转型
- 裁剪方法以提取定义的区域
- 支持双线性插值和最近邻的调整大小方法
- 顺时针旋转90°,任意角度旋转
- 垂直和水平翻转
- 形状
- 快速线条绘制算法,包括各种抗锯齿算法
- 可变的笔触厚度和笔画/印章线
- 椭圆,折线,四边形,矩形和三角形
- 三次贝塞尔曲线,基数样条曲线和闭合曲线
- 填充形状
- 快速椭圆和矩形填充方法
- 三角形,四边形,简单和复杂的多边形
- 贝塞尔曲线和基数样条曲线
- 发条
- 不同的混合模式,包括Alpha,加法,减法,乘法,遮罩和无
- 优化的快速路径,实现非混合式发条
- 筛选
- 卷积,模糊
- 亮度,对比度,伽玛调整
- 灰度/亮度,反转
- 转换次数
- 将WriteableBitmap转换为字节数组
- 从字节数组创建WriteableBitmap
- 从应用程序资源或内容轻松创建WriteableBitmap
- 从任何平台支持的图像流创建WriteableBitmap
- 将WriteableBitmap作为 TGA图像写入流
- 另存为 PNG图像的扩展方法。 在这里下载
- Windows Phone特定方法
- 保存到媒体库和相机胶卷
- 外部代码
- DrawString方法实现
Silverlight示例显示正在运行的WriteableBitmapEx:
- 的 形状的样品包括其中不同的形状绘制各种情况。默认情况下,显示了一个小样,称为“呼吸之花”。基本上会产生围绕中心环旋转的大小不同的圆。该示例还包含一个静态页面,其中显示了一些可能的形状。
- 该 填充样品开始与动画的FillCurveClosed方法的样条插值的紧张演示,再加上一些随机的动画填充的椭圆。该示例还包含一个静态页面,其中显示了一些可能的填充形状。
- 该 曲线样品展示了贝塞尔和的样条插值方法。该示例从演示动画开始,该演示动画使用Cardinal样条线DrawCurve方法绘制程序生长的人造植物。该示例的另一部分是交互式的,并允许使用鼠标绘制和操纵Beziér和Cardinal花键。有关更多信息,请参 见此博客文章。
- 所述 BLIT样品结合WriteableBitmaps和示出了整齐的颗粒的效果。
Windows Phone交互式曲线示例的视频。 外部资源: Adam Kinney提出了一个 很好的示例,该示例使用WriteableBitmapEx库动态地将撕裂的风化效果应用于照片。 Calrity Consulting的Erik Klimczak撰写了一篇有关 Advanced Animation的很好的博客文章:Silverlight中的15,000 Visuals动画。他使用WriteableBitmapEx获得最佳性能。 彼得·布隆伯格(Peter Bromberg) 用WriteableBitmapEx撰写了一篇很棒的文章,名为Silverlight 4 Martin Fractals。
性能!WriteableBitmapEx方法比XAML Shape子类快得多。例如,WriteableBitmapEx线描方法比Silverlight 线元素快20到30倍 。如果需要绘制许多形状,则WriteableBitmapEx方法是正确的选择。
使用方便!// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
using(writeableBmp.GetBitmapContext())
{
// Load an image from the calling Assembly's resources via the relative path
writeableBmp = BitmapFactory.New(1, 1).FromResource("Data/flower2.png");
// Clear the WriteableBitmap with white color
writeableBmp.Clear(Colors.White);
// Set the pixel at P(10, 13) to black
writeableBmp.SetPixel(10, 13, Colors.Black);
// Get the color of the pixel at P(30, 43)
Color color = writeableBmp.GetPixel(30, 43);
// Green line from P1(1, 2) to P2(30, 40)
writeableBmp.DrawLine(1, 2, 30, 40, Colors.Green);
// Line from P1(1, 2) to P2(30, 40) using the fastest draw line method
int[] pixels = writeableBmp.Pixels;
int w = writeableBmp.PixelWidth;
int h = writeableBmp.PixelHeight;
WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor);
// Blue anti-aliased line from P1(10, 20) to P2(50, 70) with a stroke of 5
writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue, 5);
// Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10)
writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black);
// Red rectangle from the point P1(2, 4) that is 10px wide and 6px high
writeableBmp.DrawRectangle(2, 4, 12, 10, Colors.Red);
// Filled blue ellipse with the center point P1(2, 2) that is 8px wide and 5px high
writeableBmp.FillEllipseCentered(2, 2, 8, 5, Colors.Blue);
// Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)
int[] p = new int[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 };
writeableBmp.DrawPolyline(p, Colors.Green);
// Cubic Beziér curve from P1(5, 5) to P4(20, 7)
// with the control points P2(10, 15) and P3(15, 0)
writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7, Colors.Purple);
// Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
int[] pts = new int[] { 10, 5, 20, 40, 30, 30};
writeableBmp.DrawCurve(pts, 0.5, Colors.Yellow);
// A filled Cardinal spline with a tension of 0.5
// through the points P1(10, 5), P2(20, 40) and P3(30, 30)
writeableBmp.FillCurveClosed(pts, 0.5, Colors.Green);
// Blit a bitmap using the additive blend mode at P1(10, 10)
writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive);
// Override all pixels with a function that changes the color based on the coordinate
writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100));
} // Invalidate and present in the Dispose call
// Take snapshot
var clone = writeableBmp.Clone();
// Save to a TGA image stream (file for example)
writeableBmp.WriteTga(stream);
// Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px high
var cropped = writeableBmp.Crop(5, 8, 10, 10);
// Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copy
var rotated = writeableBmp.Rotate(90);
// Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copy
var flipped = writeableBmp.Flip(FlipMode.Horizontal);
// Resizes the WriteableBitmap to 200px wide and 300px high using bilinear interpolation
var resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);
WriteableBitmapEx库起源于几篇博客文章,这些文章还详细描述了某些方面的实现和用法。博客文章可能被视为文档。 WriteableBitmap扩展方法引入了SetPixel方法。 绘图线-Silverlight WriteableBitmap扩展II提供了DrawLine方法。 绘制形状-Silverlight WriteableBitmap扩展III带来了形状功能(椭圆,折线,四边形,矩形,三角形)。 转换,编码和解码Silverlight WriteableBitmap数据附带字节数组转换方法,以及如何将WriteableBitmap编码/解码为JPEG。 与Silverlight的WriteableBitmap混合并混合提供了Blit功能。 WriteableBitmapEx-CodePlex上的WriteableBitmap扩展现已宣布该项目。 作为TGA图像的WriteableBitmap的快速和脏输出提供了原始的TgaWrite函数。 更圆,更快,更好-WriteableBitmapEx 0.9.0.0发布了0.9.0.0版,并提供了有关曲线样本的更多信息。 顺其自然-Windows Phone的WriteableBitmapEx引入了Windows Phone的WriteableBitmapEx版本和示例。 填充到破裂点-WriteableBitmapEx 0.9.5.0宣布为0.9.5.0版,其中包含有关新Fill方法的一些信息,并附带了一个不错的示例。 一张完整的位图-WinRT Metro Style的WriteableBitmapEx发布了1.0.0.0版,并提供了有关WinRT Metro Style版本的背景知识。