2012-03-25 83 views
3

可以使用DirectWrite在WinForm應用程序中將圖像文件渲染爲PictureBox嗎?SharpDX,DirectWrite和Windows窗體

我正在使用SharpDX並通過DirectWrite示例嘗試構建最簡單的工作案例。

我創建了一個窗體,並只添加了一個pictureBox。然後下面的代碼。窗體顯示,但PictureBox無法看到。

任何指導將不勝感激。

謝謝!

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
//using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using SharpDX.Direct2D1; 
using SharpDX.DXGI; 
using SharpDX; 
using SharpDX.DirectWrite; 

using AlphaMode = SharpDX.Direct2D1.AlphaMode; 
using Factory = SharpDX.Direct2D1.Factory; 

namespace d2dwTextEdit 
{ 
public partial class Form1 : Form 
{ 
    public Factory Factory2D { get; private set; } 
    public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; } 
    public WindowRenderTarget RenderTarget2D { get; private set; } 
    public SolidColorBrush SceneColorBrush { get; private set; } 
    public TextFormat TextFormat { get; private set; } 
    public SharpDX.RectangleF ClientRectangle { get; private set; } 


    public Form1() 
    { 
     InitializeComponent(); 
     Initialize(); 
     Render(); 
    } 

    protected void Initialize() 
    { 
     Factory2D = new SharpDX.Direct2D1.Factory(); 
     FactoryDWrite = new SharpDX.DirectWrite.Factory(); 

     HwndRenderTargetProperties properties = new HwndRenderTargetProperties(); 
     properties.Hwnd = pictureBox1.Handle; 
     properties.PixelSize = new System.Drawing.Size(pictureBox1.Width, pictureBox1.Height); 
     properties.PresentOptions = PresentOptions.None; 

     TextFormat = new TextFormat(FactoryDWrite, "Calibri", 30) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center }; 

     RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties); 
     RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive; 
     RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype; 

     ClientRectangle = new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height); 

     SceneColorBrush = new SolidColorBrush(RenderTarget2D, Colors.White); 
     SceneColorBrush.Color = Colors.Black;    


    } 

    private void Render() 
    { 
     RenderTarget2D.Clear(Colors.White); 
     RenderTarget2D.DrawText("Hello Marshall", TextFormat, ClientRectangle, SceneColorBrush); 
    } 

} 
} 
+0

在窗口可見之前呈現任何東西永遠不會起作用。顯示的事件是第一個顯示窗口可見的事件。改用Paint事件。 – 2012-03-25 20:19:47

+0

感謝您的幫助,Hans。不幸的是......沒有快樂。我在PaintBox和表單本身的Paint處理函數中調用了Render(),並且沒有任何變化。 – vocalionecho 2012-03-26 01:28:25

回答

6

嗨我有點跟着你的問題的例子,但我終於設法解決了,因爲我想獨立開始編寫遊戲,所有的繪製方法必須在RenderTarget類的beginDrawendDraw方法中。您還需要實施RenderLoop類,這將爲您提供回調循環委託。我的代碼粗糙的道歉。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using SharpDX.Direct3D; 
using SharpDX.Direct2D1; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
using SharpDX.DXGI; 
using SharpDX; 
using SharpDX.Windows; 
using System.Globalization; 


using SharpDX.DirectWrite; 

namespace dx11 
{ 


    public partial class Form1 : Form 
    { 
     private SharpDX.Direct3D10.Device _mDevice = null; 
     WindowRenderTarget wndRender = null; 
     SharpDX.Direct2D1.Factory fact = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded); 
     SolidColorBrush scenebrush; 
     RenderTargetProperties rndTargProperties; 
     HwndRenderTargetProperties hwndProperties; 
     SharpDX.Windows.RenderForm form = new RenderForm(); 
     RenderLoop.RenderCallback callback; 

     public Form1() 
     { 
      InitializeComponent(); 

      SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); 

      form.Width = 600; 
      form.Height = 600; 
      test(); 
      callback = new RenderLoop.RenderCallback(Render); 

      RenderLoop.Run(form, callback);        
     } 

     private void test() 
     { 
       rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)); 

       hwndProperties = new HwndRenderTargetProperties(); 
       hwndProperties.Hwnd = form.Handle; 
       hwndProperties.PixelSize = new SharpDX.DrawingSize(form.ClientSize.Width, form.ClientSize.Height); 
       hwndProperties.PresentOptions = PresentOptions.None; 
       wndRender = new WindowRenderTarget(fact, rndTargProperties, hwndProperties); 
       scenebrush = new SolidColorBrush(wndRender, Colors.Red); 
       // scenebrush.Color = Colors.Cornsilk; 
       form.Show(); 
     } 

     public void Render() 
     {  

      wndRender.BeginDraw(); 
      wndRender.Clear(Colors.DarkBlue); 
      wndRender.DrawRectangle(new SharpDX.RectangleF(10, 10, 50, 50), scenebrush, 2.00F); 
      wndRender.Flush(); 
       wndRender.EndDraw(); 
     } 

    } 
} 

這應該會在左上角創建一個帶有紅色正方形的藍色窗體。

1

你實際上並不想用HwndRenderTarget渲染「進入PictureBox」。您實際上想要渲染到PictureBox具有渲染作業的位圖中。你可以通過創建一個「共享位圖」(我不熟悉SharpDX,但底層方法是ID2D1RenderTarget :: CreateSharedBitmap()),渲染到該位置,然後在調用EndDraw()後使PictureBox無效。