繁体   English   中英

如何在 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 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM