图片处理时,往往先通过程序打开图片,根据图片大小开辟一块内存,将图片存入,再复制到数组中进行处理。现在有相机照的照片通过局域网传回计算机,程序中用一个空数组接收照片信息bmp格式,如何将该图片显示到picturebox中?并把该数组中的数据放到自己开辟的内存中?

解决方案 »

  1.   

    把数组转成流,C#的流类型可以直接从byte[]中读数据
      

  2.   

    //byte[] buffer
    MemoryStream ms= new MemoryStream(buffer);
    Bitmap flag = new Bitmap(ms);
    pictureBox1.Image = flag;
      

  3.   

    再仔细说说要求:这是通过picturebox打开图片后,开辟内存,再转存到数组中的代码:
    Bitmap bmp= (Bitmap)pictureBox21.Image;
    Bitmap result;
    result = new Bitmap(bmp);
    Rectangle rect = new Rectangle(0, 0, width, height);
    System.Drawing.Imaging.BitmapData bmpData = result.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    IntPtr ptr = bmpData.Scan0;
    int bytes = bmpData.Stride * bmpData.Height;
    byte[] rgbValues = new byte[bytes];
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);//将指针指向的内存的图复制到数组中//通过操作数组处理图片System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);//将数组中的图复制到指针指向的内存中
    result.UnlockBits(bmpData);
    pictureBox21.Image = result;//显示处理完的图片现在要求变了,图片在数组里,需要开辟一块新的空白内存来接收数组,处理,并最终显示在picturebox中,怎么做啊?
      

  4.   

    3楼,按你的代码写,这一句报错Bitmap flag = new Bitmap(ms);说是参数无效
      

  5.   


                byte []buffer =File.ReadAllBytes(@"C:\1.bmp");                        
                MemoryStream ms = new MemoryStream(buffer);
                Bitmap flag = new Bitmap((Stream)ms);
                pictureBox1.Image = flag;测试通过
      

  6.   

       Bitmap flag = new Bitmap((Stream)ms);