簡體   English   中英

WPF:以編程方式將Rectangle繪制到ImageSource實例

[英]WPF: programatically draw a Rectangle to an ImageSource instance

我是C#和WPF的新手,我可能正在研究完全錯誤的方法。 我有一個JPEG字節數組作為源。 我不能改變這個。 我需要獲取數組,執行一些計算並在JPEG區域中繪制矩形。 然后,將其寫入Image XAML控件。

我得到了JPEG,可以將其轉換為ImageSource並將其顯示為ImageControl 我找不到從ImageSource獲取繪圖上下文的方法。 我使用ImageSourceConverter讀取JPEG數組,此類返回一個ImageSource實例duh!。

ImageSource mImage = (ImageSource)mConverter.ConvertFrom(mImageBuffer);

ImageSource沒有繪圖上下文屬性。

看來我需要的是DrawingImage ,它是從ImageSource派生的,並且具有drawing context屬性。

如何使用DrawingImage代替ImageSource

我看着ImageDrawing類,它具有ImageSource屬性。 此類沒有繪圖上下文。

我目前正在研究Visual類,希望能對您有所幫助。

編輯:感謝@nefarious為我指出正確的方向。 我結束了以下內容:

ImageSource mImage = (ImageSource)mConverter.ConvertFrom(mImageBuffer);

BitmapSource bImage = mImage as BitmapSource;

// Draw a Rectangle
DrawingVisual dVisual = new DrawingVisual();
using (DrawingContext dc = dVisual.RenderOpen())
{
    dc.DrawImage(bImage, new Rect(0, 0, bImage.PixelWidth, bImage.PixelHeight));
    dc.DrawRectangle(Brushes.Green, null, new Rect(20, 20, 150, 100));
}
RenderTargetBitmap targetBitmap = new RenderTargetBitmap(640,480,96,96, PixelFormats.Default);
targetBitmap.Render(dVisual);

WriteableBitmap wBitmap = new WriteableBitmap(targetBitmap);
image.Source = wBitmap;

您是否看過使用Visuals,我不知道它們的效率如何,但是看來您將無法將源復制到ImageSource並直接繪制到其中。

創建一個DrawingVisual並將ImageSource和Rectangles繪制到繪圖視覺對象的繪圖上下文中。

然后使用WriteableBitmap在圖像中顯示它;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM