Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将bmp文件转换为pcx文件_C#_Bmp_Pcx - Fatal编程技术网

C# 如何将bmp文件转换为pcx文件

C# 如何将bmp文件转换为pcx文件,c#,bmp,pcx,C#,Bmp,Pcx,找到任何将图像格式bpm转换为pcx的方法或dll吗 我一直在尝试以下代码 public static void ConvertBMP2PCX(string bmpFilePath) { List<byte> listBytePCX = new List<byte>(); Bitmap bmp = new Bitmap(bmpFilePath); int bmpWidth = bmp.Width;

找到任何将图像格式bpm转换为pcx的方法或dll吗

我一直在尝试以下代码

 public static void ConvertBMP2PCX(string bmpFilePath)
    {
        List<byte> listBytePCX = new List<byte>();

        Bitmap bmp = new Bitmap(bmpFilePath);
        int bmpWidth = bmp.Width;
        int bmpHeight = bmp.Height;

        byte[] byteBmp;
        using (MemoryStream ms = new MemoryStream())
        {
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byteBmp = ms.ToArray();
            ms.Close();
        }

        int bytesPerLine = (bmpWidth + 7) / 8;
        int xEnd = bmpWidth - 1;
        int yEnd = bmpHeight - 1;

        byte[] header ={
        0x0A,           // "PCX File"
        0x05,           // "Version 5"
        0x01,           // RLE Encoding
        0x01,           // 1 bit per pixel
        0x00, 0x00,     // XStart at 0
        0x00, 0x00,     // YStart at 0
        (byte)(xEnd&0xFF), (byte)((xEnd>>8) & 0xFF),      // Xend
        (byte)(yEnd&0xFF), (byte)((yEnd>>8) & 0xFF),      // Yend
        (byte)(xEnd&0xFF), (byte)((xEnd>>8) & 0xFF),      // Xend
        (byte)(yEnd&0xFF), (byte)((yEnd>>8) & 0xFF),      // Yend
        0x0F, 0x0F, 0x0F, 0x0E, 0x0E, 0x0E, 0x0D, 0x0D, 0x0D, 0x0C, 0x0C, 0x0C,   //48-byte EGA palette info
        0x0B, 0x0B, 0x0B, 0x0A, 0x0A, 0x0A, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,  
        0x07, 0x07, 0x07, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04,  
        0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,  
        0x00,          // Reserved byte, always x00
        0x01,          // 1 bit plane
        (byte)(bytesPerLine&0xFF), (byte)((bytesPerLine>>8) & 0xFF),      // Bytes per scan line: (XEnd - XStart,  1) / 8
        0x01, 0x00,    // Palette type: 1 means color or monochrome
        0x00, 0x00,    // Horizontal screen size (not used)
        0x00, 0x00     // Vertical screen size (not used)
     };
        listBytePCX.AddRange(header); // Write most of header data
        listBytePCX.AddRange(new byte[54]);// pad the 128-byte header


        byte[] rowIn = new byte[bmpWidth * 3];
        int[] bits = { 128, 64, 32, 16, 8, 4, 2, 1 };

        byte[] bytes = new byte[2];
        int last = 0;
        int count = 0;
        for (int y = 0; y < bmpHeight; y++)
        {
            //getPixelRow(rowIn, y);
            int currentByteCount = (y + 1) * bytesPerLine;
            if (currentByteCount > byteBmp.Length)
            {
                currentByteCount = byteBmp.Length;
                rowIn = new byte[bmpWidth * 3];
            }

            for (int i = y * bytesPerLine; i < currentByteCount; i++)
            {
                rowIn[count] = byteBmp[i];
            }
            count = 0;

            for (int x = 0; x < bmpWidth; x += 8)
            {
                int n = x + 8;
                if (n > bmpWidth) n = bmpWidth;
                int b = 0;
                for (int j = x; j < n; j++)
                    if (rowIn[j + j + j] != 0)
                        b |= bits[j - x];
                if (last == b && count < 63)
                    count++;
                else
                {
                    if (count > 0)
                    {
                        bytes[0] = (byte)(count | 0xC0);
                        bytes[1] = (byte)last;
                        listBytePCX.Add(bytes[0]);
                        listBytePCX.Add(bytes[1]);
                    }
                    last = b;
                    count = 1;
                }
            }
            if (count > 0)
            {
                bytes[0] = (byte)(count | 0xC0);
                bytes[1] = (byte)last;
                listBytePCX.Add(bytes[0]);
                listBytePCX.Add(bytes[1]);
                count = 0;
                last = 0;
            }
        }

        //Save pcx file
        string pcxFilePath = bmpFilePath.Substring(0, bmpFilePath.LastIndexOf('.') + 1) + "1";
        using (FileStream fs = new FileStream(pcxFilePath, FileMode.Create, FileAccess.Write))
        {
            fs.Write(listBytePCX.ToArray(), 0, listBytePCX.Count);
            fs.Flush();
            fs.Close();
        }
    }
公共静态无效转换器bmp2pcx(字符串bmpFilePath)
{
List listBytePCX=新列表();
位图bmp=新位图(bmpFilePath);
int bmpWidth=bmp.宽度;
int bmpHeight=bmp.高度;
字节[]byteBmp;
使用(MemoryStream ms=new MemoryStream())
{
保存(ms,System.Drawing.Imaging.ImageFormat.bmp);
byteBmp=ms.ToArray();
Close女士();
}
int bytesPerLine=(bmpWidth+7)/8;
int xEnd=bmpWidth-1;
int-yEnd=bmpHeight-1;
字节[]头={
0x0A,/“PCX文件”
0x05,/“版本5”
0x01,//RLE编码
0x01,//每像素1位
0x00,0x00,//从0开始
0x00,0x00,//在0处开始
(字节)(xEnd&0xFF),(字节)(xEnd>>8)和0xFF),//xEnd
(byte)(yEnd&0xFF),(byte)(yEnd>>8)和0xFF),//yEnd
(字节)(xEnd&0xFF),(字节)(xEnd>>8)和0xFF),//xEnd
(byte)(yEnd&0xFF),(byte)(yEnd>>8)和0xFF),//yEnd
0x0F、0x0F、0x0F、0x0E、0x0E、0x0E、0x0D、0x0D、0x0C、0x0C、0x0C、//48字节EGA调色板信息
0x0B,0x0B,0x0B,0x0A,0x0A,0x0A,0x09,0x09,0x09,0x08,0x08,0x08,
0x07,0x07,0x07,0x06,0x06,0x06,0x05,0x05,0x05,0x04,0x04,0x04,
0x03,0x03,0x03,0x02,0x02,0x02,0x01,0x01,0x01,0x00,0x00,0x00,0x00,
0x00,//保留字节,始终为x00
0x01,//1位平面
(byte)(bytesPerLine&0xFF),(byte)((bytesPerLine>>8)和0xFF),//每扫描行字节数:(XEnd-XStart,1)/8
0x01,0x00,//调色板类型:1表示颜色或单色
0x00,0x00,//水平屏幕大小(未使用)
0x00,0x00//垂直屏幕大小(未使用)
};
listbytecx.AddRange(header);//写入大部分头数据
listbytecx.AddRange(新字节[54]);//填充128字节的头
byte[]rowIn=新字节[bmpWidth*3];
int[]位={128,64,32,16,8,4,2,1};
字节[]字节=新字节[2];
int last=0;
整数计数=0;
对于(int y=0;ybyteBmp.Length)
{
currentByteCount=字节长度;
rowIn=新字节[bmpWidth*3];
}
对于(int i=y*bytesPerLine;ibmpWidth)n=bmpWidth;
int b=0;
对于(int j=x;j0)
{
字节[0]=(字节)(计数为0xC0);
字节[1]=(字节)最后一个;
添加(字节[0]);
添加(字节[1]);
}
last=b;
计数=1;
}
}
如果(计数>0)
{
字节[0]=(字节)(计数为0xC0);
字节[1]=(字节)最后一个;
添加(字节[0]);
添加(字节[1]);
计数=0;
last=0;
}
}
//保存pcx文件
字符串pcxFilePath=bmpFilePath.Substring(0,bmpFilePath.LastIndexOf('.')+1)+“1”;
使用(FileStream fs=newfilestream(pcxFilePath,FileMode.Create,FileAccess.Write))
{
Write(listbytecx.ToArray(),0,listbytecx.Count);
fs.Flush();
fs.Close();
}
}

但它不起作用,只创建一条pcx格式的细线。

在上有一个.NET库端口。该库支持BMP和PCX格式,还支持将图像从一种格式转换为另一种格式。

该库工作正常。谢谢!很高兴我能帮忙。请随意“接受”答案:)…@AndyFong请您提供将bmp文件转换为pcx文件的代码。。我正在用C#开发一个windows应用程序。。任何想法!!提前感谢您可以按照上面提供的链接,打开选项卡文档。有一些示例代码。不确定为什么会关闭带有完整代码的问题,但代码中的问题是,将图像保存为bmp格式与从中提取实际数据字节完全不同。您可以使用
锁位
封送.复制