将位图格式更改为Format8bppIndexed C#时获取AccessViolationException

时间:2011-11-10 09:22:45

标签: c# image

下面是从fileBufferArray中的数据创建位图的函数。

public void Plot(ref byte[] fileBufferArray, int fileSize, float[] frequency, int offset, FrmEncode frmEncode, FrmMain frmMain) {
        int columns = pictureBox1.Width;
        int rows = pictureBox1.Height;
        int position = offset;
        byte currentByte;
        globalFrmMain = frmMain;
        globalOffset = offset;
        globalFileSize = fileSize;
        globalColumns = columns;


        Bitmap b = new Bitmap(columns, rows, PixelFormat.Format24bppRgb);
        BitmapData bmd = b.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadWrite, b.PixelFormat); //PixelFormat.Format24bppRgb

        // The unsafe code block is necessary to operate directly on the image
        // memory vs. setpixel.  About 250-300x faster
        unsafe {
            for (int j = 0; j < rows; j++) {
                byte* row = (byte*)bmd.Scan0 + (j * bmd.Stride); 

                for (int i = 0; i < columns; i++) {
                    currentByte = 0;
                    if (position < fileSize) {
                        currentByte = fileBufferArray[j * columns + i + offset];
                        position++;

                        //color coding
                        if (FrmEncode.colorMode == 0) { //normal
                            row[i * 3 + 1] = currentByte;
                        } else if (FrmEncode.colorMode == 1) {//ASCII
                            if ((currentByte >= 32) && (currentByte <= 127)) {
                                row[i * 3] = 255; //blue
                            } else {
                                row[i * 3] = 32; //gray
                                row[i * 3 + 1] = 32;
                                row[i * 3 + 2] = 32;                                    
                            }

                        } else if (FrmEncode.colorMode == 2) {//Frequency
                            if (frequency[currentByte]>.375){
                              row[i * 3+2] = 255; //red
                            } 
                            if (frequency[currentByte] <.01) {
                              row[i * 3] = 255; //blue
                            }

                            if ((frequency[currentByte] >= .01)&&(frequency[currentByte]<=.375))
                             {
                                row[i * 3] = 64; //gray
                                row[i * 3 + 1] = 64;
                                row[i * 3 + 2] = 64;   
                            }
                        }


                        if (FrmEncode.invert == 1) {
                            row[i * 3] = (byte)(255 - row[i * 3]);
                            row[i * 3 + 1] = (byte)(255 - row[i * 3+1]);
                            row[i * 3 + 2] = (byte)(255 - row[i * 3+2]); 
                        }
                    }                                            
                }
            }                
            b.UnlockBits(bmd);
            pictureBox1.Image = b;

           // Invert(b);
        } //unsafe

    }

我希望此功能可以制作灰度图像。所以我将位图格式更改为Format8bppIndexed但我得到了访问冲突异常

{“尝试读取或写入受保护的内存。这通常表示其他内存已损坏。”}

row[i * 3 + 1] = currentByte;

为什么要抛出此异常?

0 个答案:

没有答案