WriteableBitmapEx

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功能 添加尚未報告的新問題。

wbx_announcement.png

特徵

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特定方法
    • 保存到媒體庫和相機膠捲
  • 外部代碼

現場樣本

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版本的背景知識。 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章