Hiļ¼Thomas:
I use the code bellow to create the png file:
Code:
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[counter] = copy_dword_boundary[x, y];
counter++;
}
}
}
else
{
resultaat_byte_boundary = new byte[new_width * height];
origineel_byte_boundary.CopyTo(resultaat_byte_boundary, 0);
}
Bitmap bmp = new Bitmap(new_width, height, pixelFormat);
//string mystr = image.Elements.GetName("/ColorSpace");
//bmp.MakeTransparent(Color.White);
System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.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, ImageFormat.Png);
count++;
}
but I only get the black-white picutre, how can I get the color picutre?