C# 来自十六进制字符串的位图图像-正在添加额外字节

C# 来自十六进制字符串的位图图像-正在添加额外字节,c#,colors,bitmap,postscript,C#,Colors,Bitmap,Postscript,我有一个来自postscript文件的十六进制字符串 << /ImageType 1 /Width 986 /Height 1 /BitsPerComponent 8 /Decode [0 1 0 1 0 1] /ImageMatrix [986 0 0 -1 0 1] /DataSource < 803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d279

我有一个来自postscript文件的十六进制字符串

<< /ImageType 1
/Width 986 /Height 1
/BitsPerComponent 8
/Decode [0 1 0 1 0 1]
/ImageMatrix [986 0 0 -1 0 1]
/DataSource <
803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba648202
> /LZWDecode filter >> image } def
/LZWDecode过滤器>>图像}def
下面是我正在使用的方法。我已经注释掉了更新颜色的方法

public static void ProcessImageColourMapping()
{
    string imageDataSource = "803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba648202";
    string imageDataSourceUpdated = GetUpdatedImage(imageDataSource);
}

public static string GetUpdatedImage(string strImageDataSource)
{
    string imageDataSourceUpdated = "";

    byte[] imageBytes = StringToByteArray(strImageDataSource);
    Bitmap bitmapImage = ByteArrayToBitmap(imageBytes);
    //UpdateColour(bitmapImage);
    byte[] imageBytesUpdated = BitmapToByteArray(bitmapImage);
    imageDataSourceUpdated = ByteArrayToString(imageBytesUpdated);

    return imageDataSourceUpdated;
}

public static byte[] StringToByteArray(String imageHexString)
{
    int numberOfChars = imageHexString.Length / 2;
    byte[] byteArray = new byte[numberOfChars];
    using (var sr = new StringReader(imageHexString))
    {
        for (int i = 0; i < numberOfChars; i++)
            byteArray[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
    }
    return byteArray;
}

public static Bitmap ByteArrayToBitmap(byte[] byteArray)
{
    int width = 986; //width and height are taken from postscript file for testing a single hex string.
    int height = 1; 
    Bitmap bitmapImage = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
    BitmapData bmpData = bitmapImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);            
    try
    {                
        Marshal.Copy(byteArray, 0, bmpData.Scan0, byteArray.Length);
    }
    finally
    {
        bitmapImage.UnlockBits(bmpData);                
    }
    return bitmapImage;
}

public static byte[] BitmapToByteArray(Bitmap bitmap)
{
    BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
    int numbytes = bmpdata.Stride * bitmap.Height;
    byte[] bytedata = new byte[numbytes];
    try
    {
        Marshal.Copy(bmpdata.Scan0, bytedata, 0, numbytes);            
    }
    finally
    {
        bitmap.UnlockBits(bmpdata);
    }
    return bytedata;
}

public static string ByteArrayToString(byte[] byteArray)
{
    StringBuilder hex = new StringBuilder(byteArray.Length * 2);
    foreach (byte b in byteArray)
    {
        hex.AppendFormat("{0:x2}", b);
    }
    return hex.ToString();
}
publicstaticvoidprocessimagecolormapping()
{
字符串imageDataSource=“803FE0503824160D0784426150B864361D0F8844625138A4562D178C466351B8E4763D1F904864523924964D27944A6552B964B65D2F984C665339A4D66D379C4E6753B9E4F67D3FA0506854A25168D47A4526954B648202”;
字符串imageDataSourceUpdated=GetUpdateImage(imageDataSource);
}
公共静态字符串GetUpdateImage(字符串strImageDataSource)
{
字符串imageDataSourceUpdated=“”;
byte[]imageBytes=StringToByteArray(strImageDataSource);
位图bitmapImage=ByteArrayToBitmap(图像字节);
//更新颜色(位图图像);
byte[]imageBytesUpdated=BitmapToByteArray(bitmapImage);
imageDataSourceUpdated=ByteArrayToString(imageBytesUpdated);
返回ImageDataSourceUpdate;
}
公共静态字节[]StringToByteArray(字符串imageHexString)
{
int numberOfChars=imagehextstring.Length/2;
byte[]byteArray=新字节[numberOfChars];
使用(var sr=新的StringReader(imageHexString))
{
for(int i=0;i
问题:
在下面的代码中,我没有为传入的十六进制字符串更新任何内容
imageDataSource

将其转换为字节[],然后转换为位图,再转换为字节[],最后转换为十六进制字符串

因此,
imageDataSourceUpdated
应具有与
imageDataSource
相同的值
但是,当我最后检查
imageDataSourceUpdated
的值时,结果是:

803FE0503824160D0784426150B864361D0F8844625138A4562D178C466351B8E4763D1F904864523924964D27944A6552B964B65D2F984C665339A4D66D379C4E67E675FA05068543A25168D47A4526954BA6648202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

附加了这么多的零


请告诉我这里缺少的内容。

您正在传递一些输入字符串,但是图像的宽度(以及字节数组大小的1/4)在示例中设置为986,这将产生您观察到的行为-您实际上没有传递
986*4
字节的数据,但位图确实有那么多。因此,您将获得实际复制到位图的第一个X字节,然后是全零。换句话说,您的问题似乎与示例数据有关,而不是与方法本身有关-这些方法工作得很好。

Hi Luaan,我从客户的postscript文件中获得了此输入十六进制字符串和大小。你知道用十六进制字符串创建“合适大小”的位图的方法吗?谢谢大家!@iniki您必须在图像本身之前读取数据,类似于
40 45 1[40 0 0-45 0 45]
。这将告诉您位图的参数-前三个数字是宽度、高度和每像素字节数。然后,数据字符串的长度应为
width*height*BPP*2
,并且应创建正确大小和格式的位图。在我的示例中,这将是例如新位图(40,45,System.Drawing.Imaging.PixelFormat.Format8Bppined)“在图像本身之前读取数据”,您的意思是/ImageMatrix[986 0 0-1 0 1]请查看我的更新问题-从ps添加了整个图像def块file@iniki在您的示例中,是宽度、高度和BitsPerComponent。然而,从现在的整个定义来看,问题更加复杂,因为图像数据实际上是使用LZW压缩的。因此,您应该期望986字节的数据(986宽度,1高度,1字节/像素)。So-获取数据字符串,将其转换为字节[],解压缩(例如),创建位图作为新位图(986,1,PixelFormat.Format8Bppined)
,进行操作,获取字节[],压缩,编码到数据字符串,就完成了。您是否尝试在
ByteArrayToBitmap
方法中使用
PixelFormat.Format8Bppined
而不是
PixelFormat.Format32BppArgB