如何在 UWP C# 中设置图像和 PDF 大小

How can set Image and PDF size in UWP C#

我正在尝试在我的项目中生成一个 pdf,我在其中将图像转换为 Pdf 并从布局生成图像。生成的图像尺寸非常大,而且没有收到好的质量。 请帮我解决这个问题。

我正在程序中使用这段代码。

 RenderTargetBitmap renderTargetBitmapimg = new RenderTargetBitmap();

            var mainlayoutHeight = pageStackPanel.ActualHeight;
            var mainlayoutWidth = pageStackPanel.ActualWidth;
            int mlHeight = Convert.ToInt32(mainlayoutHeight);
            int mlWidth = Convert.ToInt32(mainlayoutWidth);


            
            await renderTargetBitmapimg.RenderAsync(pageStackPanel, mlWidth, mlHeight);
            
            ///SAVE IMAGE IN FOLDER 
            var pixelBufferimg = await renderTargetBitmapimg.GetPixelsAsync();
            var pixels = pixelBufferimg.ToArray();
            var displayInformation = DisplayInformation.GetForCurrentView();
            var storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);

            StorageFolder projectFolder = await storageFolder.CreateFolderAsync("TestImage", CreationCollisionOption.OpenIfExists);
            var file = await projectFolder.CreateFileAsync(jpgFilename, CreationCollisionOption.GenerateUniqueName);

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                     BitmapAlphaMode.Premultiplied,
                                    (uint)renderTargetBitmapimg.PixelWidth,
                                    (uint)renderTargetBitmapimg.PixelHeight,
                                    200, 200,
                                    pixels);

                await encoder.FlushAsync();
                StorageFile prescriptionJpgFile = await projectFolder.GetFileAsync(jpgFilename);
                await stream.FlushAsync();
                stream.Seek(0);
                stream.Dispose();
                renderTargetBitmapimg = null;

答案是否定的,您不能设置生成的图像文件的大小。但是您可以尝试通过指定特定的宽度和高度来调整它的大小以减小其物理尺寸。您可以使用 BitmapEncoder class 对原始图像进行编码,更改图像的 BitmapInterpolationMode。代码如下所示:

    //open file as stream
        using (IRandomAccessStream fileStream = await imagefile.OpenAsync(FileAccessMode.ReadWrite))
        {
            var decoder = await BitmapDecoder.CreateAsync(fileStream);

            var resizedStream = new InMemoryRandomAccessStream();

            BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
            double widthRatio = (double)reqWidth / decoder.PixelWidth;
            double heightRatio = (double)reqHeight / decoder.PixelHeight;

            double scaleRatio = Math.Min(widthRatio, heightRatio);

            if (reqWidth == 0)
                scaleRatio = heightRatio;

            if (reqHeight == 0)
                scaleRatio = widthRatio;

            uint aspectHeight = (uint)Math.Floor(decoder.PixelHeight * scaleRatio);
            uint aspectWidth = (uint)Math.Floor(decoder.PixelWidth * scaleRatio);

            encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;

            encoder.BitmapTransform.ScaledHeight = aspectHeight;
            encoder.BitmapTransform.ScaledWidth = aspectWidth;

            await encoder.FlushAsync();
            resizedStream.Seek(0);
            var outBuffer = new byte[resizedStream.Size];
            await resizedStream.ReadAsync(outBuffer.AsBuffer(), (uint)resizedStream.Size, InputStreamOptions.None);


            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            Debug.WriteLine(storageFolder.Path);
            StorageFile sampleFile = await storageFolder.CreateFileAsync("testfile.pdf", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteBytesAsync(sampleFile, outBuffer);

        }

有关更多信息,您可以查看此文档:How to Compress image and change its Size

另一个建议是您可以只使用 UWP 打印 API 来生成 PDF 文件。您可以查看 Windows Community Toolkit 的 Print Helper。您可以选择不打印,而是将其保存为 PDF 文件。