c# - 带镜像输出的打印 JPG 图像
问题描述
嗨,伙计们:我可以使用 SDk 进行打印,并且图像以正确的尺寸打印,但图像是镜像的。
我怎样才能解决这个问题?下面的代码有什么错误?
public bool PrintImage(string imgPath) {
using (Bitmap img = new Bitmap(imgPath)) {
IntPtr rawPtr = convertImageToRaw(img);
return Api.SendImageData(portNumber, rawPtr, 0, 0, img.Width, img.Height);
}
}
private IntPtr convertImageToRaw(Bitmap bmp) {
int width = bmp.Width;
int height = bmp.Height;
Bitmap targetBmp;
Bitmap newBmp = new Bitmap(bmp);
targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format24bppRgb);
BitmapData bmpData = targetBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, targetBmp.PixelFormat);
int bytes = Math.Abs(bmpData.Stride) * bmpData.Height;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(bmpData.Scan0, rgbValues, 0, bytes);
GCHandle pinnedArray = GCHandle.Alloc(rgbValues, GCHandleType.Pinned);
IntPtr result = pinnedArray.AddrOfPinnedObject();
newBmp.RotateFlip(RotateFlipType.RotateNoneFlipNone);
bmp.Save(System.IO.Path.Combine(@"C:\\Users\\Pictures\\images\\", "test123.jpg"));
targetBmp.Save(System.IO.Path.Combine(@"C:\\Users\\Pictures\\images\\", "test1234.jpg"));
newBmp.Save(System.IO.Path.Combine(@"C:\\Users\\Pictures\\images\\", "test1235.jpg"));
newBmp.Dispose();
return result;
}
解决方案
看来,您的原始照片包含 EXIF 元数据记录。其中,它可以包含如何在显示之前处理图像的附加说明。一些应用程序/SDK 确实尊重该指令,而其他应用程序/SDK 则默默地忽略 EXIF - 这就是您可以收到镜像等内容的原因。
有 8 个可能的 EXIF 方向值,编号为 1 到 8。
- 0 度 – 方向正确,无需调整。
- 0 度,镜像 - 图像已从后到前翻转。
- 180 度 - 图像颠倒。
- 180 度,镜像 - 图像倒置并从后向前翻转。
- 90 度 - 图像在其一侧。
- 90 度,镜像 - 图像在其一侧并从后向前翻转。
- 270 度 - 图像在其远端。
- 270 度,镜像 - 图像在其远端并从后向前翻转。
推荐阅读
- protocol-buffers - 如何根据 protobuf grpc 规范正确格式化响应对象
- docker - 用于 Windows SQL Server 的 Docker 上的 Net Core 无法正常工作
- javascript - 更改传单中的图层 z-index
- c++ - 使用 Qt 信号和槽传递指针
- javascript - 在 Typescript / Javascript 中过滤掉深层嵌套数据结构的对象
- reactjs - 如何处理导致重新渲染的有用参数和函数
- java - 我想从 Eclipse 连接到 SSMS 数据库,但它抛出 SQLserverException
- android-studio - 'getView' 不覆盖任何内容
- python - Matplotlib ,Dimensione grafico impostata ...标签“tagliati”
- redux - 根据另一个可观察值的值延迟发出的可观察值