2011-11-14 10 views
1

現在、System.Drawing.BitmapをWPF WriteableBitmapに統合する際にいくつか問題があります。System.Drawing.BitmapをWriteableBitmapの領域にコピー

からWriteableBitmapの位置(X、Y)にコピーしたいと思います。

次のコードは、これをどうしようとしたかを示しています。

BitmapData Data = Bitmap.LockBits(new Rectangle(0, 0, Bitmap.Width, Bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
WriteableBitmap.Lock(); 

//CopyMemory(WriteableBitmap.BackBuffer, Data.Scan0, ImageBufferSize); 

Int32Rect Rect = new Int32Rect(X, Y, Bitmap.Width, Bitmap.Height); 
WriteableBitmap.AddDirtyRect(Rect); 
Bitmap.UnlockBits(Data); 
Bitmap.Dispose();` 

どうもありがとう、

Neokript

答えて

3

使用WritableBitmap.WritePixels。これにより、アンマネージコードの使用が防止されます。

BitmapData Data = Bitmap.LockBits(new Rectangle(0, 0, Bitmap.Width, Bitmap.Height), 
    ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

try 
{  
    WritableBitmap.WritePixels(
     new Int32Rect(0,0,Bitmap.Width, Bitmap.Height), 
     Data.Scan0, 
     Data.Stride, 
     X, Y); 
} 
finally 
{ 
    Bitmap.UnlockBits(Data); 
} 

Bitmap.Dispose(); 
1

あなたはBitmapDataWriteableBitmapの両方をロックする必要があります。特定の(x、y)位置に画像を描画する場合は、描画する画像の残りの幅と高さも管理する必要があります。

[DllImport("kernel32.dll",EntryPoint ="RtlMoveMemory")] 
public static extern void CopyMemory(IntPtr dest, IntPtr source,int Length); 

public void DrawImage(Bitmap bitmap) 
{ 
    BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

    try 
    { 
     writeableBitmap.Lock(); 
     CopyMemory(writeableBitmap.BackBuffer, data.Scan0, 
      (writeableBitmap.BackBufferStride * bitmap.Height)); 
     writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.Width, bitmap.Height)); 
     writeableBitmap.Unlock(); 
    } 
    finally 
    { 
     bitmap.UnlockBits(data); 
     bitmap.Dispose(); 
    } 
} 

そして、あなたのコード内で:

Bitmap bitmap = new Bitmap("pic.jpg"); // obtain it from anywhere, memory, file, stream ,... 

writeableBitmap = new WriteableBitmap(
         bitmap.Width, 
         bitmap.Height, 
         96, 
         96, 
         PixelFormats.Pbgra32, 
         null); 

imageBox.Source = writeableBitmap; 

DrawImage(bitmap); 

私はこの方法を使用して29 fpsので1080Pクリップをレンダリングするために管理しています。

関連する問題