我有一个来自postscript文件的Hex字符串。
<< /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
以下是我使用的方法。我已经注释了更新颜色的方法。
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();
}
问题:
在下面的代码中,我没有为传入的十六进制字符串imageDataSource
更新任何内容
将其转换为byte [] - 然后转换为Bitmap - 返回byte [] - 最后返回Hex字符串。
因此,imageDataSourceUpdated
应与imageDataSource
具有相同的价值
但是,当我最终检查imageDataSourceUpdated
的值时,它会显示为:
803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba64820200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
.....
附加了很多零。
请指导我在这里缺少的东西。
答案 0 :(得分:1)
您正在传递一些输入字符串,但图像的宽度(以及字节数组大小的1/4)在样本中设置为986,这将产生您观察到的行为 - 您实际上并没有传递986 * 4
个字节的数据,但Bitmap确实有那么多。因此,您将获得实际复制到位图的前X个字节,然后全部为0。换句话说,您的问题似乎是您的样本数据,而不是方法本身 - 这些工作正常。