当前位置:编程学习 > C#/ASP.NET >>

怎么把位图保存为byte数组,求救!!


 string fileName = Application.StartupPath.ToString() + "\\ticket.bmp";
            Bitmap bmp = new Bitmap(Image.FromFile(fileName));
           BitmapData bitmapData = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);//是传这个吗?
           byte[] BGRValues = new byte[bitmapData.Stride * bitmapData.Height];
           IntPtr Ptr = bitmapData.Scan0;
           System.Runtime.InteropServices.Marshal.Copy(Ptr, BGRValues, 0, BGRValues.Length);
           bmp.UnlockBits(bitmapData);
运行后BGRValues数组里的值全是255,哪里有问题呢?谢谢 --------------------编程问答-------------------- 没用过,看看msdn的实例吧~~

http://msdn.microsoft.com/zh-cn/library/5ey6h79d(v=vs.110).aspx --------------------编程问答-------------------- 红色部分换成 

bmp.获取它的格式(bmp属性) --------------------编程问答--------------------
引用 2 楼 hwenycocodq520 的回复:
红色部分换成 

bmp.获取它的格式(bmp属性)

已经改成这样bmp.PixelFormat,直接读取它的格式的,但是BGRValues数组里还是255,应该不对吧,怎么可能每个字节都是一样的呢?求救 --------------------编程问答-------------------- 谁说全部是255的,你都没有输出过,你只看调试器上几个数字,因为图片大部分是白色的(255,255,255),黑色只占比较少部分
你可以

int j = 0;
for (int i = 0; i < BGRValues.Length; i++)
{
     if (BGRValues[i] != 255) j++;
}
MessageBox.Show(j+"");

看到有几万个不是255的没? --------------------编程问答-------------------- 全是0,是不是看错了,你提供的那张图,BGRValues[1146]之前全部是255,和你的图有关,换张图,第一个就不是255了。 

void btnBMP_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(tbSource.Text.Trim());

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int i = 0; i < rgbValues.Length; i++)
            {
                if (rgbValues[i] != 255)
                {
                    MessageBox.Show(i.ToString());
                    break;
                }
            }
            /*
            // Set every third value to 255. A 24bpp bitmap will look red.  
            for (int counter = 2; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.

            // Draw the modified image.
            e.Graphics.DrawImage(bmp, 0, 150);*/
            bmp.UnlockBits(bmpData);
        }
--------------------编程问答--------------------
引用 4 楼 hwenycocodq520 的回复:
谁说全部是255的,你都没有输出过,你只看调试器上几个数字,因为图片大部分是白色的(255,255,255),黑色只占比较少部分
你可以

int j = 0;
for (int i = 0; i < BGRValues.Length; i++)
{
     if (BGRValues[i] != 255) j++;
}
MessageBox.Show(j+"");

看到有几万个不是255的没?

对的,是有几万个不是255的,另外我想问下我加载图片用
  string fileName = Application.StartupPath.ToString() + "\\ticket.bmp";
            Bitmap bmp = new Bitmap(Image.FromFile(fileName));

              Bitmap bmp = new Bitmap(“ticket.bmp");
有区别吗? --------------------编程问答-------------------- 一个是相对路径,一个是绝对路径,后一句会在执行程序目录中找那个图片 --------------------编程问答--------------------
Quote: 引用 7 楼 hwenycocodq520 的回复:

一个是相对路径,一个是绝对路径,后一句会在执行程序目录中找那个图片[/quote
我用这两种方式加载的图片后长度不一样怎么回事?另外请问怎么知道到底图片保存为byte数组有没有正确呢? --------------------编程问答-------------------- 用内存流直接就取出来了
        /// <summary>
        /// 缩小图片
        /// </summary>
        /// <param name="strOldPic">源图文件名(包括路径)</param>
        /// <param name="intWidth">缩小至宽度</param>
        /// <param name="intHeight">缩小至高度</param>
        public static byte[] SmallPic(string strOldPic, int intWidth, int intHeight)
        {

            System.Drawing.Bitmap objPic, objNewPic;
            try
            {
                objPic = new System.Drawing.Bitmap(strOldPic);
                objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
                System.IO.MemoryStream mstream = new System.IO.MemoryStream();
                objNewPic.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
                return mstream.ToArray();
            }
            catch (Exception exp) {

                Logger.DebugLog(exp.Message);
                return new byte[0];
            
            }
            finally
            {
                objPic = null;
                objNewPic = null;
                GC.Collect();
            }
        }
这是我之前写的缩放,并且导出byte[] 你可以参考一下 --------------------编程问答--------------------
引用 5 楼 cheery_an 的回复:
全是0,是不是看错了,你提供的那张图,BGRValues[1146]之前全部是255,和你的图有关,换张图,第一个就不是255了。 

void btnBMP_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(tbSource.Text.Trim());

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int i = 0; i < rgbValues.Length; i++)
            {
                if (rgbValues[i] != 255)
                {
                    MessageBox.Show(i.ToString());
                    break;
                }
            }
            /*
            // Set every third value to 255. A 24bpp bitmap will look red.  
            for (int counter = 2; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.

            // Draw the modified image.
            e.Graphics.DrawImage(bmp, 0, 150);*/
            bmp.UnlockBits(bmpData);
        }

试了下你的也是一样,我要的就是这个图的,现在我要实现图片保存为btye数组后传进一个函数里把图片打在车票上,但是返回总是有问题,不知道是不是保存btye时出错了。但是也看不出来有没有出错 --------------------编程问答-------------------- 下面是我反编译过来的代码,大家帮我看看吧,谢谢!
    byte[] array = new byte[] { 0x31, 0x33, 0x36, 50, 0x30, 0x3d, 0x31 };
    byte[] buffer2 = new byte[0x30];
    buffer2[0] = 0x35;
    buffer2[1] = 50;
    buffer2[2] = 0x30;
    buffer2[3] = 0x34;
    buffer2[4] = 0x33;
    for (int i = 5; i < buffer2.Length; i++)
    {
        buffer2[i] = 0x30;
    }
    buffer2[buffer2.Length - 1] = 0x31;
    int num2 = ((this.bitmap.Width * this.bitmap.Height) / 8) + 9;
    byte[] sourceArray = new byte[num2];
    BitmapData bitmapdata = this.bitmap.LockBits(new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height), ImageLockMode.ReadOnly, this.bitmap.PixelFormat);
    IntPtr ptr1 = bitmapdata.Scan0;
    int num3 = this.bitmap.Width * this.bitmap.Height;
    byte num5 = 0;
    byte num6 = 0;
    int num7 = 9;
    for (int j = 0; j < num3; j++)
    {
        byte num4;
        if (Marshal.ReadByte(bitmapdata.Scan0, j) > 0x80)
        {
            num4 = 0;
        }
        else
        {
            num4 = 1;
        }
        if (num5 == 0)
        {
            num6 = 0;
        }
        num6 = (byte) (num6 ^ ((byte) (num4 << (7 - num5))));
        num5 = (byte) (num5 + 1);
        if (num5 > 7)
        {
            num5 = 0;
            sourceArray[num7++] = (byte) (sourceArray[num7++] + Convert.ToByte(num6));
        }
    }
    this.bitmap.UnlockBits(bitmapdata);//到这应该就是我一开始发的代码
    string str = string.Format("{0:D3}", this.bitmap.Height);
    sourceArray[0] = 0x37;
    sourceArray[1] = 0x36;
    sourceArray[2] = 0x30;
    sourceArray[3] = 0x30;
    sourceArray[4] = 0x30;
    sourceArray[5] = 0x36;
    sourceArray[6] = (byte) str[0];
    sourceArray[7] = (byte) str[1];
    sourceArray[8] = (byte) str[2];
    Array.Resize<byte>(ref array, array.Length + sourceArray.Length);
    Array.Copy(sourceArray, 0, array, array.Length - sourceArray.Length, sourceArray.Length);//到这不知道是干什么的?
    KGB_WRAPPER_RESULT kgb_wrapper_result = this.device.kgb.WriteSetUmsgCmd(array);//btye数组传进来。
    if (this.device.kgb.ExecSendUserMessage() != KGB_WRAPPER_RESULT.ERR_OK)
--------------------编程问答-------------------- 没人知道了吗?求救 --------------------编程问答--------------------

Image image = Image.FromFile("文件名");
byte[] buffer = new byte[image.Width * image.Height];
MemoryStream ms = new MemoryStream(buffer);
image.Save(ms, image.RawFormat);
ms.Close();
ms.Dispose();
image.Dispose();
--------------------编程问答-------------------- byte[] to image

MemoryStream ms = new MemoryStream(buffer);
Image image=Image.FromStream(ms);
//....
//...
--------------------编程问答-------------------- 现在不是bmp保存为byte的问题,这个我一开始发的代码应该是对的,麻烦帮看看我11楼发的反编译的代码,看看在传byte数组到函数里之前还需要做什么,谢谢 --------------------编程问答--------------------
引用 2 楼 hwenycocodq520 的回复:
红色部分换成 

bmp.获取它的格式(bmp属性)
你从哪里学的编程  教教我
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,