Сконвертировать изображение из bmp8 в bmp 24 - C#
Формулировка задачи:
Добрый день, друзья.
Нависла проблема, с которой не могу разобраться.
Как указано в теме, необходимо создать некий конвертер из bmp8 в bmp24, при этом не используя стандартные средства(типа PixelFormat), а работая со структурой формата bmp..
Прошу помощи..
Решение задачи: «Сконвертировать изображение из bmp8 в bmp 24»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
FileStream f, f2;
//byte[] buf = new byte[55];
byte buf,buf2, buf3, buf4;
int [] a = new int[54];
string bfsizehex;
string bisizehex;
int[,] p = new int[256, 4];
int[,] palete = new int[256, 3];
int bfsize, bisize, nol = 0;
int[] newz = new int[54];
int ost24, ost8, x, y;
public Form1()
{
InitializeComponent();
}
/*public void HexToint(string h, float result)
{
int c = Convert.ToInt32(h);
if (c > 0)
{
result -= 1;
}
}*/
private void button1_Click(object sender, EventArgs e)
{
ost24 = 0;
OpenFileDialog opf = new OpenFileDialog();
if (opf.ShowDialog() == DialogResult.OK)
{
f = new FileStream(opf.FileName, FileMode.Open);
}
for (int i = 0; i < 54; i++)
{
buf = (byte)f.ReadByte();
a[i] = buf;
}
if (a[28] != 8)
{
MessageBox.Show("Выбранный файл должен быть 8 бит");
button2.Enabled = false;
}
else
{
MessageBox.Show("Выбран файл");
button2.Enabled = true;
}
f.Position = 54;
for (int k = 0; k < 256; k++)
{
for (int l = 0; l < 4; l++)
{
buf = (byte)f.ReadByte();
p[k, l] = buf;
}
}
for (int m = 0; m < 256; m++)
{
for (int n = 0; n < 3; n++)
{
palete[m, n] = p[m, n];
}
}
string file_wight = Convert.ToString(Convert.ToInt32(Convert.ToString(a[21], 2) + Convert.ToString(a[20], 2) + Convert.ToString(a[19], 2) + Convert.ToString(a[18], 2)));
string file_height = Convert.ToString(Convert.ToInt32(Convert.ToString(a[25], 2) + Convert.ToString(a[24], 2) + Convert.ToString(a[23], 2) + Convert.ToString(a[22], 2)));
x = Convert.ToInt32(file_wight, 2);
y = Convert.ToInt32(file_height, 2);
ost24 = x * 3 % 4;
ost8 = x % 8;
if (ost24 != 0)
{
ost24 = 4 - ost24;
}
if (ost8 != 0)
{
ost8 = 4 - ost8;
}
bfsize = (x * 3 + ost24) * y + 54;
bfsizehex = Convert.ToString(bfsize, 2);
for (int o = 0; o < 54; o++)
{
newz[o] = a[o];
}
newz[5] = Convert.ToInt32(bfsizehex[1] + bfsizehex[2]);
newz[4] = Convert.ToInt32(bfsizehex[3] + bfsizehex[4]);
newz[3] = Convert.ToInt32(bfsizehex[5] + bfsizehex[6]);
newz[2] = Convert.ToInt32(bfsizehex[7] + bfsizehex[8]);
newz[13]=0;//Вписываем в заголовок новое смещение
newz[12]=0;
newz[11]=0;
newz[10]=54;
newz[29]=0; //Вписываем в заголовок новую глубину
newz[28]=24;
bisize = bfsize - 54;
bisizehex = Convert.ToString(bisize, 2);
if (ost24 != 0)
{
newz[37] = Convert.ToInt32(bisizehex[1] + bisizehex[2]);
newz[36] = Convert.ToInt32(bisizehex[3] + bisizehex[4]);
newz[35] = Convert.ToInt32(bisizehex[5] + bisizehex[6]);
newz[34] = Convert.ToInt32(bisizehex[7] + bisizehex[8]);
}
else
{
newz[37] = 0;
newz[36] = 0;
newz[35] = 0;
newz[34] = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
int s;
SaveFileDialog save = new SaveFileDialog();
save.Filter = "BMP(*.bmp)|*.bmp";
if (save.ShowDialog() == DialogResult.OK)
{
f2 = new FileStream(save.FileName, FileMode.Create);
}
buf2 = 0;
s = 0;
while (s != 54)
{
buf2 = (byte)newz[s];
f2.WriteByte(buf2);
s += 1;
}
f.Position = 1078;
f2.Position = 54;
for (int i = 0; i <= y; i++)
{
double a = (i / y) * 100;
progressBar1.Value = (int)Math.Round(a);
f.Position = (x + ost8) * (i - 1) + 1078;
for (int j = 0; j < x; j++)
{
buf3 = (byte)f.ReadByte();
for (int k = 0; k < 3; k++)
{
buf4 = (byte)palete[buf3, k];
f2.WriteByte(buf4);
}
if (j == x && ost24 != 0)
{
for (int l = 0; l <= ost24; l++)
{
f2.WriteByte((byte)nol);
}
}
}
}
}
}
}