位图来自十六进制字符串的图像 - 正在添加的额外字节 [英] Bitmap Image from Hex string - extra bytes being added

查看:306
本文介绍了位图来自十六进制字符串的图像 - 正在添加的额外字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < / 1将ImageType 
/宽度986 /身高1
/ BitsPerComponent 8
/解码[0 1 0 1 0 1]
/ ImageMatrix [986 0 0 -1 0 1]
/ DataSource<
803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba648202
> / LZWDecode filter>> image}下面是我使用的方法。下面是我使用的方法。我注释掉更新颜色的方法。

 公共静态无效ProcessImageColourMapping()
{
串imageDataSource =803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba648202;
string imageDataSourceUpdated = GetUpdatedImage(imageDataSource);
}

公共静态字符串GetUpdatedImage(字符串strImageDataSource)
{
串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 byteArray [i] = Convert.ToByte (new char [2] {(char)sr.Read(),(char)sr.Read()}),16);
}
return byteArray;
}

public static Bitmap ByteArrayToBitmap(byte [] byteArray)
{
int width = 986; //宽度和高度取自postscript文件,用于测试单个十六进制字符串。
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(byteArray中的字节b)
{
hex.AppendFormat({0:x2},b);
}
return hex.ToString();
}

问题:

在下面的代码中,我没有更新任何东西进来的十六进制字符串 imageDataSource

将其转换为byte [] - 然后到位图 - 返回字节[ ] - 最后回到十六进制字符串。



所以, imageDataSourceUpdated 应该与 imageDataSource

但是,当我最终检查 imageDataSourceUpdated 的值时,它显示为:



803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba64820200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 .....结果
这么多的零被追加。



可以指导我在这里丢失的内容。

解决方案

在你的示例中,图像的宽度(以及字节数组大小的1/4)被设置为986,这将产生你观察到的行为 - 你实际上并不传递 986 * 4 字节的数据,但Bitmap确实有这么多。所以你会得到你实际复制到位图的第一个X字节,然后全部零。换句话说,看来你的问题是你的示例数据,而不是方法本身 - 这些工作很好。


I have a Hex string that's coming from postscript file.

<< /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

Below are the methods that I am using. I have commented out the method for updating color.

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();
}

Issue:
In below code, I am not updating anything for incoming Hex string imageDataSource.
Converting it to byte[] - then to Bitmap - Back to byte[] - and finally back to Hex string.

So, imageDataSourceUpdated should have same value as imageDataSource.
However, when I finally check the value for imageDataSourceUpdated, it comes out as:

803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba64820200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.....
So many zeros being appended.

Can please guide what I am missing here.

解决方案

You are passing some input string, but the width of the image (and so 1/4 of the size of the byte array) is set to 986 in your sample, which would yield the behaviour you observe - you're not actually passing 986 * 4 bytes of data, but the Bitmap does have that many. So you'll get the first X bytes you actually copied to the bitmap, and then all zeros. In other words, it seems your issue is with your sample data, not with the methods themselves - those work just fine.

这篇关于位图来自十六进制字符串的图像 - 正在添加的额外字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆