我創建了一個庫,可以從C#中的輸入數據創建Postscript文件。但是,使用以下代碼創建圖像時,圖像顯示不正確。這就像他們偏離了中心。從左到右,首先看到圖像的右側邊緣,然後圖像的左側顯示在圖像的剩餘分配空間中。爲了清楚起見,這就像是用「你好」這個詞,而不是顯示「ohell」。C#圖像到Postscript最終偏離中心
我正在使用下面的C#代碼來創建一個List對象,其中填充直接輸出到postscript文件的行。它看起來很標準,但我注意到我需要編碼的幾個特性。出於某種原因,我的75寬度圖像希望顯示爲76像素的寬度,所以我添加了該部分來爲其添加像素。我不知道這是爲什麼,我還沒有做足夠的測試,看看這一切都搞砸了。此外,通常情況下,我使用的圖像矩陣具有負高度,其次是高度,但圖像看起來顛倒,所以我不得不將其交換。
public virtual List<string> AddImage(Image img, int x, int y, int height, int width)
{
string sDataSource;
List<string> destinationList = new List<string>();
int iImgWidth = img.Width;
if (iImgWidth % 2 != 0)
{
iImgWidth += 1;
}
using (MemoryStream ms2 = new MemoryStream())
{
Bitmap bmp = (img as Bitmap).Clone(
new Rectangle(0, 0, img.Width, img.Height),
PixelFormat.Format24bppRgb);
bmp.Save(ms2, ImageFormat.Bmp);
sDataSource = $"<{string.Join("", ms2.ToArray().Select(a => a.ToString("X2")))}>";
}
destinationList.Add($"{x} {y} translate");
destinationList.Add($"{width} {height} scale");
destinationList.Add(
$"/DeviceRGB setcolorspace {{ << /ImageType 1 /Width {iImgWidth} /Height {img.Height} /ImageMatrix [{iImgWidth} 0 0 {img.Height} 0 {img.Height}] /BitsPerComponent 8 /Decode [0 1 0 1 0 1] ");
destinationList.Add($"/DataSource {sDataSource} >> image }} exec ");
return destinationList;
}
我得到的輸出縮小到下面,它仍然顯示不正確。
%!PS-Adobe-3.1
0 649 translate
27 20 scale
/DeviceRGB setcolorspace { << /ImageType 1 /Width 76 /Height 56 /ImageMatrix [76 0 0 56 0 -56] /BitsPerComponent 8 /Decode [0 1 0 1 0 1]
/DataSource <... (hex code)> >> image } exec
showpage
任何人都可以點我在正確的方向?我一直在爲此掙扎好幾天。
我已將示例代碼上傳到SourceForge,並在「文件」部分包含示例輸入和輸出。 https://sourceforge.net/projects/postscript-image-import-test/ –