2011-09-20 83 views
2

創建的圖像與在週期和規模PictureBox.Width * N從陣列LockBits和高度:C#移動位圖

using (var bmp = new Bitmap(len, _height, PixelFormat.Format24bppRgb)) 
{ 
      var data = bmp.LockBits(new Rectangle(0, 0, len, _height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
      var bytes = data.Stride * data.Height; 
      var rgb = new byte[bytes]; 
      var ptr = data.Scan0; 
      Marshal.Copy(data.Scan0, rgb, 0, bytes); 


      // …fill array „rgb「 

      Marshal.Copy(rgb, 0, ptr, bytes); 
      bmp.UnlockBits(data); 

      g = _pictureBox.CreateGraphics(); 
      g.InterpolationMode = InterpolationMode.Default; 
      g.DrawImage(bmp, _pictureBox.Width - len * _scaleWidth, 0, len * _scaleWidth, _pictureBox.Height); 
} 

在下一iterateration:

Graphics g; 
     using (var bmp = new Bitmap(_pictureBox.Image)) 
     { 
      g = _pictureBox.CreateGraphics(); 
      g.InterpolationMode = InterpolationMode.Default; 
      g.DrawImage(_pictureBox.Image, new RectangleF(0, 0, _pictureBox.Width - len * _scaleWidth, _pictureBox.Height), new RectangleF(len * _scaleWidth, 0, _pictureBox.Width * _scaleWidth - len, _height), GraphicsUnit.Pixel); 
     } 
     g.Dispose(); 

簡而言之:我剪切並複製了可能會改變圖像的部分圖像,但沒有任何結果。是否有可能因爲上一步的規模? 也許我錯了。建議將算法轉移並添加新的位圖。

+1

你必須指定位圖到_pictureBox.Image財產第一,不是分配給它nessecary。我建議你使用Control.CreateGraphics()方法,並直接在表單上寫入。 –

+0

通過附加到控件上的MouseClick和MouseMove事件,可以使用我的Win32 Interop,您可以將miage固定到鼠標指針,並在窗體上按住左按鈕時訂閱其他位圖的鼠標移動。我將編輯我的帖子以添加示例。 –

回答

1

我建議你在Form實例上使用Control.CreateGraphics()方法,並直接在Form上寫,因爲PaintBox控件速度很慢。

嘗試使用我的助手功能,這將讓你使用使用Win32互操作StretchBlt(拉伸)或BitBlt的(無拉伸)你的位圖的一部分:

使用範例:

Graphics graphics = ...; 
graphics.GdiDrawImage 
(
    image, 
    new Rectangle(
     (int)rectangle.Left, 
     (int)rectangle.Top, 
     (int)rectangle.Width, 
     (int)rectangle.Height 
    ), 
    0, 0, image.Width, image.Height 
); 

的源代碼:

public static class GraphicsHelper 
{ 
    public static void GdiDrawImage(this Graphics graphics, Bitmap image, Rectangle rectangleDst, int nXSrc, int nYSrc, int nWidth, int nHeight) 
    { 
     IntPtr hdc = graphics.GetHdc(); 
     IntPtr memdc = GdiInterop.CreateCompatibleDC(hdc); 
     IntPtr bmp = image.GetHbitmap(); 
     GdiInterop.SelectObject(memdc, bmp); 
     GdiInterop.SetStretchBltMode(hdc, 0x04); 
     GdiInterop.StretchBlt(hdc, rectangleDst.Left, rectangleDst.Top, rectangleDst.Width, rectangleDst.Height, memdc, nXSrc, nYSrc, nWidth, nHeight, GdiInterop.TernaryRasterOperations.SRCCOPY); 
     //GdiInterop.BitBlt(..) put it here, if you did not mention stretching the source image 
     GdiInterop.DeleteObject(bmp); 
     GdiInterop.DeleteDC(memdc); 
     graphics.ReleaseHdc(hdc); 
    } 
} 

public class GdiInterop 
{ 
    /// <summary> 
    /// Enumeration for the raster operations used in BitBlt. 
    /// In C++ these are actually #define. But to use these 
    /// constants with C#, a new enumeration _type is defined. 
    /// </summary> 
    public enum TernaryRasterOperations 
    { 
     SRCCOPY = 0x00CC0020, // dest = source 
     SRCPAINT = 0x00EE0086, // dest = source OR dest 
     SRCAND = 0x008800C6, // dest = source AND dest 
     SRCINVERT = 0x00660046, // dest = source XOR dest 
     SRCERASE = 0x00440328, // dest = source AND (NOT dest) 
     NOTSRCCOPY = 0x00330008, // dest = (NOT source) 
     NOTSRCERASE = 0x001100A6, // dest = (NOT src) AND (NOT dest) 
     MERGECOPY = 0x00C000CA, // dest = (source AND pattern) 
     MERGEPAINT = 0x00BB0226, // dest = (NOT source) OR dest 
     PATCOPY = 0x00F00021, // dest = pattern 
     PATPAINT = 0x00FB0A09, // dest = DPSnoo 
     PATINVERT = 0x005A0049, // dest = pattern XOR dest 
     DSTINVERT = 0x00550009, // dest = (NOT dest) 
     BLACKNESS = 0x00000042, // dest = BLACK 
     WHITENESS = 0x00FF0062, // dest = WHITE 
    }; 

    /// <summary> 
    /// Enumeration to be used for those Win32 function 
    /// that return BOOL 
    /// </summary> 
    public enum Bool 
    { 
     False = 0, 
     True 
    }; 

    /// <summary> 
    /// Sets the background color. 
    /// </summary> 
    /// <param name="hdc">The HDC.</param> 
    /// <param name="crColor">Color of the cr.</param> 
    /// <returns></returns> 
    [DllImport("gdi32.dll")] 
    public static extern int SetBkColor(IntPtr hdc, int crColor); 

    /// <summary> 
    /// CreateCompatibleDC 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC); 

    /// <summary> 
    /// DeleteDC 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern Bool DeleteDC(IntPtr hdc); 

    /// <summary> 
    /// SelectObject 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true)] 
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); 

    /// <summary> 
    /// DeleteObject 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern Bool DeleteObject(IntPtr hObject); 

    /// <summary> 
    /// CreateCompatibleBitmap 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hObject, int width, int height); 

    /// <summary> 
    /// BitBlt 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern Bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); 

    /// <summary> 
    /// StretchBlt 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern Bool StretchBlt(IntPtr hObject, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hObjSource, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, TernaryRasterOperations dwRop); 

    /// <summary> 
    /// SetStretchBltMode 
    /// </summary> 
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] 
    public static extern Bool SetStretchBltMode(IntPtr hObject, int nStretchMode); 
}