首页 > 解决方案 > 带镜像输出的打印 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;
    }

标签: c#wpfwindows-10jpeg

解决方案


看来,您的原始照片包含 EXIF 元数据记录。其中,它可以包含如何在显示之前处理图像的附加说明。一些应用程序/SDK 确实尊重该指令,而其他应用程序/SDK 则默默地忽略 EXIF - 这就是您可以收到镜像等内容的原因。

EXIF 方向值

有 8 个可能的 EXIF 方向值,编号为 1 到 8。

  1. 0 度 – 方向正确,无需调整。
  2. 0 度,镜像 - 图像已从后到前翻转。
  3. 180 度 - 图像颠倒。
  4. 180 度,镜像 - 图像倒置并从后向前翻转。
  5. 90 度 - 图像在其一侧。
  6. 90 度,镜像 - 图像在其一侧并从后向前翻转。
  7. 270 度 - 图像在其远端。
  8. 270 度,镜像 - 图像在其远端并从后向前翻转。

在此处输入图像描述


推荐阅读