Thx Thomas for the assistance, but I can't find the color palette
Nor in the image itself (it's not colorspace is it?), nor in the pdf itself.
So I'm still getting "wrong" colors in my output image
This is the code i use to change dword-byte array (trick is that I put it in a 2dimensional arrary first, with ending caracters and then reput it in a 1dimensional array) in case someone needs it:
Code:
static void ExportAsPngImage(PdfDictionary image, ref int count)
{
int width = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.Height);
int bitsPerComponent = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.BitsPerComponent);
System.Drawing.Imaging.PixelFormat pixelFormat = new System.Drawing.Imaging.PixelFormat();
PdfSharp.Pdf.PdfArray arr = image.Elements.GetArray(PdfSharp.Pdf.Advanced.PdfImage.Keys.ColorSpace);
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}
PdfSharp.Pdf.Filters.FlateDecode fd = new PdfSharp.Pdf.Filters.FlateDecode();
byte[] origineel_byte_boundary = image.Stream.UnfilteredValue;//fd.Decode(image.Stream.Value);
byte[] resultaat_byte_boundary = null;
int new_width = width;
int allignment = 4;
if (new_width % allignment != 0)
//Image data in BMP files always starts at a DWORD boundary, in PDF it starts at a BYTE boundary.
//Most images have a width that is a multiple of 4, so there is no problem with them.
//You must copy the image data line by line and start each line at the DWORD boundary.
{
while (new_width % allignment != 0)
new_width++;
byte[,] copy_dword_boundary = new byte[height, new_width];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < new_width; x++)
{
if (x <= width && (x + (y * width) != origineel_byte_boundary.Length ) )
// while not at end of line, take orignale array
copy_dword_boundary[y, x] = origineel_byte_boundary[x + (y * width)];
else //fill new array with ending 0
copy_dword_boundary[y, x] = 0;
}
}
resultaat_byte_boundary = new byte[new_width * height];
int counter = 0;
for (int x = 0; x < copy_dword_boundary.GetLength(0); x++)
{
for (int y = 0; y < copy_dword_boundary.GetLength(1); y++)
{ //put 2dim array back in 1dim array
resultaat_byte_boundary[teller] = copy_dword_boundary[x, y];
counter ++;
}
}
}
else
{
resultaat_byte_boundary = new byte[new_width * height];
origineel_byte_boundary.CopyTo(resultaat_byte_boundary, 0);
}
System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, new_width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, pixelFormat);
System.Runtime.InteropServices.Marshal.Copy(resultaat_byte_boundary, 0, bmd.Scan0, resultaat_byte_boundary.Length);
bmp.UnlockBits(bmd);
using (FileStream fs = new FileStream("C:\\" + String.Format("Image{0}.png", count), FileMode.Create, FileAccess.Write))
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}