kolya5544

bmp rle gen

Feb 3rd, 2021 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace BitmapRLEGenerator
  7. {
  8.     class Program
  9.     {
  10.         public static int ResX = 1000;
  11.         public static int ResY = 1000;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             FileStream bmp = new FileStream("bmp.bmp", FileMode.Create, FileAccess.ReadWrite);
  16.             bmp.Write(new byte[2] { 0x42, 0x4D }); //"BM"
  17.  
  18.             List<byte> bbb = new List<byte>();
  19.  
  20.             int CommandAmountPerLine = ResX / 0xFF; if (CommandAmountPerLine * 0xFF != ResX) { CommandAmountPerLine++; }
  21.  
  22.             for (int i = 0; i < ResY; i++)
  23.             {
  24.                 for (int z = 1; z < CommandAmountPerLine+1; z++)
  25.                 {
  26.                     if (ResX - (z * 255) > 0)
  27.                     {
  28.                         bbb.Add(0xFF);
  29.                         bbb.Add(0xFF);
  30.                     } else
  31.                     {
  32.                         bbb.Add((byte)(ResX % 0xFF));
  33.                         bbb.Add(0xFF);
  34.                     }
  35.                 }
  36.                 bbb.Add(0x00);
  37.                 bbb.Add(0x00);
  38.             }
  39.             bbb.RemoveRange(bbb.Count - 2, 2);
  40.             bbb.Add(0x00);
  41.             bbb.Add(0x01);
  42.             int BmpSize = 54 + bbb.Count;
  43.  
  44.             bmp.Write(Bi(BmpSize)); //entire file size
  45.             bmp.Write(Bi(0)); //unused
  46.             bmp.Write(Bi(54)); //offset where bmp data is
  47.             bmp.Write(Bi(40)); //size of DIB header
  48.             bmp.Write(Bi(ResX));
  49.             bmp.Write(Bi(ResY));
  50.             bmp.Write(Bi(524289)); //plane count and bits per color
  51.             bmp.Write(Bi(1)); //rle encoding used
  52.             bmp.Write(Bi(bbb.Count)); //size of bmp data
  53.             bmp.Write(Bi(2835)); //Some printer info idk
  54.             bmp.Write(Bi(2835)); //Some printer info idk
  55.             bmp.Write(Bi(256)); //color stuff
  56.             bmp.Write(Bi(256)); //color stuff
  57.             bmp.Write(bbb.ToArray());
  58.             bmp.Flush();
  59.         }
  60.  
  61.         private static byte[] Bi(int z)
  62.         {
  63.             return BitConverter.GetBytes(z);
  64.         }
  65.     }
  66. }
  67.  
Add Comment
Please, Sign In to add comment