具有镜像输出的打印 JPG 图像

Printed JPG image with mirrored output

大家好:我可以使用 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 元数据记录。其中,它可以包含如何在显示之前处理图像的附加说明。有些人 apps/SDKs 确实尊重该说明,其他人则默默地忽略 EXIF - 这就是为什么你可以收到镜像等东西的原因。

EXIF orientation values

There are 8 possible EXIF orientation values, numbered 1 to 8.

  1. 0 degrees – the correct orientation, no adjustment is required.
  2. 0 degrees, mirrored – image has been flipped back-to-front.
  3. 180 degrees – image is upside down.
  4. 180 degrees, mirrored – image is upside down and flipped back-to-front.
  5. 90 degrees – image is on its side.
  6. 90 degrees, mirrored – image is on its side and flipped back-to-front.
  7. 270 degrees – image is on its far side.
  8. 270 degrees, mirrored – image is on its far side and flipped back-to-front.

我通过 rotate108FlipX 找到了解决方案。

 img.RotateFlip(RotateFlipType.Rotate180FlipX);
 IntPtr rawPtr = convertImageToRaw(img);
 return Api.SendImageData(portNumber, rawPtr, 0, 0, img.Width, img.Height);