Beispiel #1
0
        public void SaveMinimapBlock(string outputDir, int nLayer, int uX, int uY, Bitmap bmp)
        {
	        string fileName = Path.Combine(outputDir, string.Format("{0}_{1}_{2}plus.tga", nLayer, uY, uX));        	

	        if (!IsBmpDataNull(bmp))
	        {
		        Rectangle rectangle = new Rectangle(0, 0, bmp.Width, bmp.Height);
		        BitmapData bitmapData = bmp.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                // 如果目标目录不存在则先创建目录
                if (!Directory.Exists(outputDir))
                {
                    Directory.CreateDirectory(outputDir);
                }

                FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate);
                byte[] buffer;
                List<byte> byteList = new List<byte>();
                		 
       		    TgaFileHeader tgaFileHeader = new TgaFileHeader();       
		        tgaFileHeader.Width = (short)bitmapData.Width;
		        tgaFileHeader.Height = (short)bitmapData.Height;
		        tgaFileHeader.PixelDep = 24;

                if (bitmapData.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    tgaFileHeader.PixelDep = 32;
                }

		        tgaFileHeader.ImageType = 2;
		        tgaFileHeader.Desc = 8;

                // 写入header
                byteList.Add(tgaFileHeader.IDLength);
                byteList.Add(tgaFileHeader.ColorMapType);
                byteList.Add(tgaFileHeader.ImageType);
                byteList.AddRange(tgaFileHeader.ColorMapSpec);
                buffer = BitConverter.GetBytes(tgaFileHeader.X);
                byteList.AddRange(buffer);
                buffer = BitConverter.GetBytes(tgaFileHeader.Y);
                byteList.AddRange(buffer);
                buffer = BitConverter.GetBytes(tgaFileHeader.Width);
                byteList.AddRange(buffer);
                buffer = BitConverter.GetBytes(tgaFileHeader.Height);
                byteList.AddRange(buffer);
                byteList.Add(tgaFileHeader.PixelDep);
                byteList.Add(tgaFileHeader.Desc);
                
                // 需要用Interop来移动数据,真麻烦
                IntPtr ptr = bitmapData.Scan0;
                byte[] bmpBuffer = new byte[(int)(bitmapData.Width * bitmapData.Stride)];
                System.Runtime.InteropServices.Marshal.Copy(ptr, bmpBuffer, 0, bmpBuffer.Length);

                int blockSize = bitmapData.Width * tgaFileHeader.PixelDep / 8;

                for (int i = 0; i < bmp.Height; i++)
                {
                    for (int j = 0; j < blockSize; j++)
                    {
                        byteList.Add(bmpBuffer[(bmp.Height - i - 1) * bitmapData.Stride + j]);
                    }
                }

                buffer = byteList.ToArray();
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();       
         		
                // 需要用Interop来移动数据,真麻烦
                System.Runtime.InteropServices.Marshal.Copy(bmpBuffer, 0, ptr, bmpBuffer.Length);

		        bmp.UnlockBits(bitmapData);
	        }
        }
Beispiel #2
0
        public void SaveMinimapBlock(string outputDir, int nLayer, int uX, int uY, Bitmap bmp)
        {
            string fileName = Path.Combine(outputDir, string.Format("{0}_{1}_{2}plus.tga", nLayer, uY, uX));

            if (!IsBmpDataNull(bmp))
            {
                Rectangle  rectangle  = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData bitmapData = bmp.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                // 如果目标目录不存在则先创建目录
                if (!Directory.Exists(outputDir))
                {
                    Directory.CreateDirectory(outputDir);
                }

                FileStream  fs = new FileStream(fileName, FileMode.OpenOrCreate);
                byte[]      buffer;
                List <byte> byteList = new List <byte>();

                TgaFileHeader tgaFileHeader = new TgaFileHeader();
                tgaFileHeader.Width    = (short)bitmapData.Width;
                tgaFileHeader.Height   = (short)bitmapData.Height;
                tgaFileHeader.PixelDep = 24;

                if (bitmapData.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    tgaFileHeader.PixelDep = 32;
                }

                tgaFileHeader.ImageType = 2;
                tgaFileHeader.Desc      = 8;

                // 写入header
                byteList.Add(tgaFileHeader.IDLength);
                byteList.Add(tgaFileHeader.ColorMapType);
                byteList.Add(tgaFileHeader.ImageType);
                byteList.AddRange(tgaFileHeader.ColorMapSpec);
                buffer = BitConverter.GetBytes(tgaFileHeader.X);
                byteList.AddRange(buffer);
                buffer = BitConverter.GetBytes(tgaFileHeader.Y);
                byteList.AddRange(buffer);
                buffer = BitConverter.GetBytes(tgaFileHeader.Width);
                byteList.AddRange(buffer);
                buffer = BitConverter.GetBytes(tgaFileHeader.Height);
                byteList.AddRange(buffer);
                byteList.Add(tgaFileHeader.PixelDep);
                byteList.Add(tgaFileHeader.Desc);

                // 需要用Interop来移动数据,真麻烦
                IntPtr ptr       = bitmapData.Scan0;
                byte[] bmpBuffer = new byte[(int)(bitmapData.Width * bitmapData.Stride)];
                System.Runtime.InteropServices.Marshal.Copy(ptr, bmpBuffer, 0, bmpBuffer.Length);

                int blockSize = bitmapData.Width * tgaFileHeader.PixelDep / 8;

                for (int i = 0; i < bmp.Height; i++)
                {
                    for (int j = 0; j < blockSize; j++)
                    {
                        byteList.Add(bmpBuffer[(bmp.Height - i - 1) * bitmapData.Stride + j]);
                    }
                }

                buffer = byteList.ToArray();
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();

                // 需要用Interop来移动数据,真麻烦
                System.Runtime.InteropServices.Marshal.Copy(bmpBuffer, 0, ptr, bmpBuffer.Length);

                bmp.UnlockBits(bitmapData);
            }
        }