PMG图片转化为bitmap图片,

ROS发送来的地图都是PMG的格式的图片,wpf并不支持,找了很久找到了转换的方法。

 

public class PgmImage
    {
        public int width;
        public int height;
        public int maxVal;
        public byte[][] pixels;
        public static int SplitUnit = 3000; //按2000* 2000pix分割图片 不要修改此值
        public PgmImage(int width, int height, int maxVal, byte[][] pixels)
        {
            this.width = width;
            this.height = height;
            this.maxVal = maxVal;
            this.pixels = pixels;
        }
        public static PgmImage LoadImage(string file)
        {
            FileStream ifs = new FileStream(file, FileMode.Open);
            BinaryReader br = new BinaryReader(ifs);

            string magic = NextNonCommentLine(br);
            if (magic != "P5")
                throw new Exception("Unknown magic number: " + magic);

            string widthHeight = NextNonCommentLine(br);
            string[] tokens = widthHeight.Split(' ');
            int width = int.Parse(tokens[0]);
            int height = int.Parse(tokens[1]);

            string sMaxVal = NextNonCommentLine(br);
            int maxVal = int.Parse(sMaxVal);

            byte[][] pixels = new byte[height][];
            for (int i = 0; i < height; ++i)
                pixels[i] = new byte[width];

            for (int i = 0; i < height; ++i)
                for (int j = 0; j < width; ++j)
                    pixels[i][j] = br.ReadByte();

            br.Close();
            ifs.Close();

            PgmImage result = new PgmImage(width, height, maxVal, pixels);
            return result;
        }
        private static string NextAnyLine(BinaryReader br)
        {
            string s = "";
            byte b = 0; // dummy
            while (b != 10) // newline
            {
                b = br.ReadByte();
                char c = (char)b;
                s += c;
            }
            return s.Trim();
        }

        private static string NextNonCommentLine(BinaryReader br)
        {
            string s = NextAnyLine(br);
            while (s.StartsWith("#") || s == "")
                s = NextAnyLine(br);
            return s;
        }

        public static List<string> MakeBitmap(string dir, int mag, string file, out int width, out int height, out string mainImagePath, BackgroundWorker worker)
        {
            PgmImage pgmImage = LoadImage(file);
            List<string> listPics = new List<string>();
            width = pgmImage.width * mag;
            height = pgmImage.height * mag;
            
            long total = width * height;
            long counter = 0;
            int pro = 0;

            int splitUnit = PgmImage.SplitUnit / mag;

            //Bitmap bitmapMain = new Bitmap(width, height);
            //Graphics grMain = Graphics.FromImage(bitmapMain);

            for (int i = 0; i < pgmImage.width / splitUnit + 1; i++)
            {
                int tempWidth = splitUnit * mag;
                if (i == pgmImage.width / splitUnit)
                    tempWidth = width % (splitUnit * mag);
                else
                    tempWidth = splitUnit * mag;

                for (int j = 0; j < pgmImage.height / splitUnit + 1; j++)
                {
                    int tempHeight = splitUnit * mag;
                    if (j == pgmImage.height / splitUnit)
                        tempHeight = height % (splitUnit * mag);
                    else
                        tempHeight = splitUnit * mag;

                    Bitmap bitmap = new Bitmap(tempWidth, tempHeight);

                    Graphics gr = Graphics.FromImage(bitmap);

                    for (int m = j * splitUnit; m < Math.Min(pgmImage.height, (j + 1) * splitUnit); ++m)
                    {
                        for (int k = i * splitUnit; k < Math.Min(pgmImage.width, (i + 1) * splitUnit); ++k)
                        {
                            int pixelColor = pgmImage.pixels[m][k];
                            System.Drawing.Color c = System.Drawing.Color.FromArgb(pixelColor, pixelColor, pixelColor);
                            SolidBrush sb = new SolidBrush(c);
                            gr.FillRectangle(sb, (k- i* splitUnit) * mag, (m - j * splitUnit) * mag, mag, mag);

                            //grMain.FillRectangle(sb, k * mag, m * mag, mag, mag);

                            counter = counter + (mag * mag);

                            int temppro = (int)((counter * 100) / total);

                            if (pro != temppro)
                            {
                                pro = temppro;
                                worker.ReportProgress(pro);
                            }
                                
                        }
                    }

                    FileInfo info = new FileInfo(file);
                    string saveFile = dir + Guid.NewGuid().ToString() + info.Name.Replace("pgm", "bmp");
                    bitmap.Save(saveFile);

                    listPics.Add(saveFile);

                    bitmap.Dispose();
                    gr.Dispose();
                }
            }

            FileInfo infoMain = new FileInfo(file);
            string saveFileMain = dir + infoMain.Name.Replace("pgm", "bmp");
            //bitmapMain.Save(saveFileMain);
            
            //bitmapMain.Dispose();
            //grMain.Dispose();
            mainImagePath = saveFileMain;
             return listPics;
        }

        private static byte[] Bitmap2Byte(Bitmap bitmap)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Jpeg);
                byte[] data = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(data, 0, Convert.ToInt32(stream.Length));
                return data;
            }
        }

        ///   <summary>   
         ///   从图片中截取部分生成新图   
         ///   </summary>   
         ///   <param   name= "sFromFilePath "> 原始图片 </param>   
         ///   <param   name= "saveFilePath "> 生成新图 </param>   
         ///   <param   name= "width "> 截取图片宽度 </param>   
         ///   <param   name= "height "> 截取图片高度 </param>   
         ///   <param   name= "spaceX "> 截图图片X坐标 </param>   
         ///   <param   name= "spaceY "> 截取图片Y坐标 </param>   
        public static List<string> SplitImage(string dir, List<byte[]> datas)
        {
            List<string> files = new List<string>();

            foreach (byte[] item in datas)
            {
                MemoryStream ms1 = new MemoryStream(item);
                Bitmap bm = (Bitmap)Image.FromStream(ms1);
                ms1.Close();

                string fileName = string.Format("{0}{1}.bmp", dir, Guid.NewGuid().ToString());
                bm.Save(fileName);

                files.Add(fileName);
            }

            return files;
        }
    }

 

转载于:https://www.cnblogs.com/RRev/p/7130973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值