using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO; 
//一帧图片数据保存在txt文件中
//从文本文件读出来,绘制在Panel控件中
//但是用下面的代码绘制后什么也看不到...
namespace test
{
    public partial class Form1 : Form
    {
        public Rectangle SrcRect, DstRect;
        IntPtr hdd;
        public Control myControl = new Control(); //创建对象
        public BITMAPINFOHEADER bmiHeader;
              
            public void drawOpen()
            {
                //hdd赋值
                this.hdd = DrawDibOpen();                bool b = DrawDibBegin(hdd,
                    IntPtr.Zero,
                    -1,
                    -1, 
                    ref this.bmiHeader, 
                    176, 
                    144, 
                    0);
            }            public void Draw(byte[] data, IntPtr hdcPanel)
            {
                //hdc赋值
                IntPtr hdcPanel2 = GetDC(panel1.Handle); 
                IntPtr Hdc = GetDC(IntPtr.Zero);
                //Graphics g = Graphics.FromHdc(Hdc);
                               
               using (Graphics g = this.myControl.CreateGraphics())
               // using (Graphics g = this.panel1.CreateGraphics())                
                {
                    this.SrcRect = this.DstRect = new Rectangle(0, 0, 176, 144);
                    g.DrawRectangle(new Pen(Color.Red,2), this.Control.ClientRectangle);                    
                    IntPtr hdc = g.GetHdc(); 
                                        
                    this.bmiHeader.biCompression = 0;
                    this.bmiHeader.biPlanes = 1;
                    this.bmiHeader.biBitCount = 24;
                    this.bmiHeader.biSize = 40;
                    this.bmiHeader.biWidth = 176;
                    this.bmiHeader.biHeight = 144;
                       
             //绘图           
             bool b = DrawDibDraw(
             hdd,
             hdc,
             10,
             20,
             -1,
             -1,
             ref this.bmiHeader,
             data,
             0,
             0,
             176,
             144,
             8);  //DDF_SAME_DRAW
               
             g.ReleaseHdc(hdc);
                }
            }        //绘制图片
        private void button2_Click(object sender, EventArgs e)
        {     
            //文本文件中保存一帧图片的数据,读出来放在byte数组中    
            byte[] buffer = System.IO.File.ReadAllBytes(@"d:\videoFrame.txt");            
            this.drawOpen();
            this.Draw(buffer, panel1.Handle);
        }
    }
}

解决方案 »

  1.   

    DrawDibBegin和DrawDibDraw返回值都为true;不知道是不是hdc获取的值有问题,
    感觉hdc值比较大 hdc = 1090591572
      

  2.   

    //找了一段别的代码,也不可以使用....[DllImport("MsVfW32.dll")] 
    public static extern bool DrawDibDraw(IntPtr hdd,IntPtr hdc,int xDst,int yDst,int dxDst,int dyDst,IntPtr lpbi,IntPtr Bits,int xSrc,int ySrc,int dxSrc,int dySrc,int wFlags); 
    [StructLayout(LayoutKind.Sequential)] public struct VIDEOHDR 

    [MarshalAs(UnmanagedType.I4)] public int lpData; 
    [MarshalAs(UnmanagedType.I4)] public int dwBufferLength; 
    [MarshalAs(UnmanagedType.I4)] public int dwBytesUsed; 
    [MarshalAs(UnmanagedType.I4)] public int dwTimeCaptured; 
    [MarshalAs(UnmanagedType.I4)] public int dwUser; 
    [MarshalAs(UnmanagedType.I4)] public int dwFlags; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)] public int[] dwReserved; 

    FrameCallBack是一个回调函数,当捕获到一帧视频时,会自动触发该函数执行。 private void FrameCallBack(IntPtr lwnd, IntPtr lpVHdr) 

    AVICAP.VIDEOHDR videoHeader = new AVICAP.VIDEOHDR(); 
    videoHeader = (AVICAP.VIDEOHDR)AVICAP.GetStructure(lpVHdr,videoHeader); AVICAP.BITMAPINFOHEADER btComeepre = new serverWin.AVICAP.BITMAPINFOHEADER(); 
    if(videoHeader.dwBytesUsed!=0) 

    byte[] VideoData = new byte[videoHeader.dwBytesUsed]; 
    AVICAP.Copy(videoHeader.lpData ,VideoData); 
    capGetVideoFormat(this.lwndC,ref btComeepre,AVICAP.SizeOf(btComeepre)); 
    display(mControlTo,mWidthTo,mHeightTo,VideoData,btComeepre); 
    } } 
    public void display(IntPtr handle, int width,int height,byte[] _bmpDate,AVICAP.BITMAPINFOHEADER _btm) 

    //hdc是绘图控件句柄 
    IntPtr hdc =AVICAP.GetDC(handle); 
    //hdd是设备环境 
    IntPtr hdd=AVICAP.DrawDibOpen(); 
    //下面代码操作内存 
    unsafe 

    //获取图象头指针 
    IntPtr phead = Marshal.AllocHGlobal(sizeof(AVICAP.BITMAPINFOHEADER)); 
    //将参数图象头结构拷贝到非托管区,phead指针指向该区域 
    Marshal.StructureToPtr(_btm,phead,true); //为BMP数据分配指针 
    IntPtr pdata = Marshal.AllocHGlobal(_btm.biSizeImage); 
    //将参数BMP图象数组拷贝到非托管内存区,pdata指针指向该区域 
    Marshal.Copy(_bmpDate,0,pdata,_btm.biSizeImage); try 

    //执行这个函数就要出异常,为什么? 
    AVICAP.DrawDibDraw(hdd,hdc,0,0,width,height,phead,pdata,0,0,_btm.biWidth,_btm.biHeight,AVICAP.DDF_SAME_DRAW|AVICAP.DDF_SAME_HDC); 

    catch(Exception ex) 

    throw ex; 
    } AVICAP.DrawDibEnd(hdd); 
    AVICAP.DrawDibClose(hdd);