如何将bmp文件转换为pcx文件 [英] How to Convert bmp file to pcx file

查看:163
本文介绍了如何将bmp文件转换为pcx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否找到将图像格式bpm转换为pcx的方法或dll?

Any method or dll found to convert the image format bpm to pcx?

我一直在尝试遵循以下代码.

I have been trying to following code.

 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();
        }
    }

但是它不起作用,只能以pcx格式创建一条细线.

But it doesn't work,only create a thin line with pcx format.

推荐答案

ImageMagick 的端口.a的.NET库,网址为 http://magick.codeplex.com/.该库支持BMP和PCX格式,还可以将图像从一种格式转换为另一种格式.

There is a port of the ImageMagick library for .NET at http://magick.codeplex.com/. The library supports both the BMP and the PCX format and also converting images from one format to another.

这篇关于如何将bmp文件转换为pcx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆