Beispiel #1
0
        public static void SetPixels(WIC_WRITABLE dstBmp, int dstX, int dstY, PointerBitmap srcPtr)
        {
            if (dstX < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(dstX));
            }
            if (dstY < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(dstY));
            }
            if (dstX + srcPtr.Width > dstBmp.PixelWidth)
            {
                throw new ArgumentOutOfRangeException(nameof(srcPtr.Width));
            }
            if (dstY + srcPtr.Height > dstBmp.PixelHeight)
            {
                throw new ArgumentOutOfRangeException(nameof(srcPtr.Height));
            }

            if (!TryGetExactPixelFormat(dstBmp.Format, out var dstFmt))
            {
                throw new Diagnostics.PixelFormatNotSupportedException(dstBmp.Format, nameof(dstBmp));
            }

            if (srcPtr.PixelFormat == dstFmt)
            {
                var rect = new System.Windows.Int32Rect(dstX, dstY, dstBmp.PixelWidth, dstBmp.PixelHeight);

                dstBmp.WritePixels(rect, srcPtr.Pointer, srcPtr.Info.BitmapByteSize, srcPtr.Info.StepByteSize);
                return;
            }

            try
            {
                dstBmp.Lock();

                var nfo    = new BitmapInfo(dstBmp.PixelWidth, dstBmp.PixelHeight, dstFmt, dstBmp.BackBufferStride);
                var dstPtr = new PointerBitmap(dstBmp.BackBuffer, nfo);

                dstPtr.AsSpanBitmap().SetPixels(0, 0, srcPtr.AsSpanBitmap());

                var w     = Math.Min(dstBmp.PixelWidth, srcPtr.Width);
                var h     = Math.Min(dstBmp.PixelHeight, srcPtr.Height);
                var drect = new System.Windows.Int32Rect(0, 0, w, h);

                dstBmp.AddDirtyRect(drect);
            }
            finally
            {
                dstBmp.Unlock();
            }
        }
        static public void Save16BitTiff(string filePath, ushort[] pixels, int width, int height)
        {
            var rgb16 = new System.Windows.Media.Imaging.WriteableBitmap(width, height, 96.0, 96.0,
                                                                         System.Windows.Media.PixelFormats.Rgb48, null);

            rgb16.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), pixels, width * 2 * 3, 0);

            var encoder = new System.Windows.Media.Imaging.TiffBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rgb16));
            using (var stream = System.IO.File.OpenWrite(filePath)) {
                encoder.Save(stream);
            }
        }