我正在使用 https://code.google.com/p/slimdx/issues/detail?id=707 上的代码将System.Drawing.Bitmap转换为SlimDX.Direct2D.Bitmap。我以前使用过 EXACTLY 相同的代码,如果工作正常。现在,当我用
Dim TheBitmap As SlimDX.Direct2D.Bitmap = LoadBitmap(New Bitmap(New MemoryStream(TheBytesThatIGotOffTheNetworkStream)))
SlimDX.Direct2D.WindowRenderTarget.DrawBitmap(TheBitmap)
我得到一个被缩放了 1.5 到 2 倍的位图。如果我保存System.Drawing.Bitmap,它就完美了。我以前做过这个,但它从未扩展过。我什至尝试摆脱流并从硬盘加载位图。它没有用。谢谢。
我的代码:
表格1.vb:
Imports SlimDX
Imports System.Text
Imports System.Threading
Imports System.IO
Public Class Form1
Dim F As Direct2D.Factory
Dim WRTP As Direct2D.WindowRenderTargetProperties
Dim WRT As Direct2D.WindowRenderTarget
Dim TheBitmap As Direct2D.Bitmap
Function LoadBitmap(drawingBitmap As Bitmap) As Direct2D.Bitmap
Dim result As Direct2D.Bitmap = Nothing
Dim drawingBitmapData As BitmapData = drawingBitmap.LockBits(New Rectangle(0, 0, drawingBitmap.Width, drawingBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
Dim dataStreamxx As DataStream = New DataStream(drawingBitmapData.Scan0, drawingBitmapData.Stride * drawingBitmapData.Height, True, False)
Dim properties As Direct2D.BitmapProperties = New Direct2D.BitmapProperties()
properties.PixelFormat = New Direct2D.PixelFormat(DXGI.Format.R8G8B8A8_UNorm, Direct2D.AlphaMode.Premultiplied)
result = New Direct2D.Bitmap(WRT, New Size(drawingBitmap.Width, drawingBitmap.Height), dataStreamxx, drawingBitmapData.Stride, properties)
drawingBitmap.UnlockBits(drawingBitmapData)
Return result
End Function
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
F = New Direct2D.Factory()
WRTP = New Direct2D.WindowRenderTargetProperties()
WRTP.Handle = Me.Handle
WRTP.PixelSize = Me.PixelSize
WRT = New Direct2D.WindowRenderTarget(F, WRTP)
End Sub
Public Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
WRT.BeginDraw()
WRT.Clear()
If Not IsNothing(TheBitmap) Then
WRT.DrawBitmap(TheBitmap)
End If
WRT.EndDraw()
End Sub
Sub Main()
Dim Command As TCP.Command
While True
If TCP.Listener.Pending() Then
TCP.Clients.Add(TCP.Listener.AcceptTcpClient())
TCP.Streams.Add(TCP.Clients.Last().GetStream())
End If
For a=0 To TCP.Clients.Count-1
If TCP.Streams(a).DataAvailable Then
Command=TCP.ReadCommand(TCP.Streams(a))
If Encoding.ASCII.GetString(Command.Command) = "Bitmap"
TheBitmap=LoadBitmap(New Bitmap(New MemoryStream(Command.SubCommands(0))) 'Convert it.
Me.Invalidate()
End If
End If
Next
End While
End Sub
End Class
我解决了。不是真的,但我只是再次翻译了它,完全相同,它有效。