在 silverlight 中缩放 PNG 图像

发布于 2024-11-17 11:01:14 字数 213 浏览 3 评论 0原文

在我的应用程序中,我接受用户的图像。如果图像超过指定大小,那么我将缩小到适当的大小并保存在数据库中。我正在使用 FJCore 库来缩放图像。该库可以很好地处理 JPEG 图像。但它不支持PNG图像。看来图书馆最近没有更新。知道如何在 Silverlight 中做到这一点吗?

In my application, I am accepting an image from user. If the image is more than specified size, then I am scaling down to appropriate size and saving in database. I am using FJCore library for scaling image. The library works well with JPEG images. But it does not support PNG images. It seems that library is not being updated recently. Any idea how can this be done in Silverlight?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-11-24 11:01:14

您可以做的是创建一个新的 Image 元素并将其源设置为从流创建的可写位图,但不要将此 Image 元素添加到可视化树中。创建另一个具有所需最终大小的 WriteableBitmap,然后在此 WriteableBitmap 上调用 render,传递 Image 元素和 ScaleTransform,以将图像大小调整为适当的大小。然后,您可以使用第二个 WriteableBitmap 作为第二个 Image 元素的源,并将其添加到可视化树中。

What you can do is create a new Image element and set its source to a Writeable bitmap created from the stream but don't add this Image element to the visual tree. Create another WriteableBitmap of the final size you want and then call render on this WriteableBitmap passing the Image element and a ScaleTransform to resize the image to the appropriate size. You can then use the second WriteableBitmap as the source for a second Image element and add that to the visual tree.

早乙女 2024-11-24 11:01:14

我使用 WriteableBitmapEx 项目确实实现了这一点。这是代码,如果有人需要的话。

private void ShowCustomImageButton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.Multiselect = false;
        openDialog.Filter = "PNG Files|*.PNG";
        bool? userClickedOK = openDialog.ShowDialog();
        if (userClickedOK == true)
        {                
            BitmapImage image = new BitmapImage();
            // get image that user has selected.
            image.SetSource(openDialog.File.OpenRead());
            WriteableBitmap wrtbmp = new WriteableBitmap(image);
            // resize image if needed.
            wrtbmp = wrtbmp.Resize(64, 64, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
            var img = wrtbmp.ToImage();
            // convert image into file stream.
            Stream filestram = img.ToStream();
            filestram.Position = 0;
            using (filestram)
            {
                // convert file stream into memory stream.
                var memoryStream = new MemoryStream();
                byte[] aryBuffer = new byte[16384];
                int nRead = filestram.Read(aryBuffer, 0, aryBuffer.Length);
                while (nRead > 0)
                {
                    memoryStream.Write(aryBuffer, 0, nRead);
                    nRead = filestram.Read(aryBuffer, 0, aryBuffer.Length);
                }
                // use following line to convert in bytes and save into database.
                memoryStream.ToArray();
                imgCustomImage.Source = CreateBitmapImage(memoryStream);
            }                        
        }
    }

    private BitmapImage CreateBitmapImage(MemoryStream memoryStream)
    {
        if ((memoryStream == null) || (memoryStream.Length == 0))
            return null;         

        var image = new BitmapImage();
        image.SetSource(memoryStream);
        return image;      
    }

I used WriteableBitmapEx project do acheive this. Here is the code if someone needs it.

private void ShowCustomImageButton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.Multiselect = false;
        openDialog.Filter = "PNG Files|*.PNG";
        bool? userClickedOK = openDialog.ShowDialog();
        if (userClickedOK == true)
        {                
            BitmapImage image = new BitmapImage();
            // get image that user has selected.
            image.SetSource(openDialog.File.OpenRead());
            WriteableBitmap wrtbmp = new WriteableBitmap(image);
            // resize image if needed.
            wrtbmp = wrtbmp.Resize(64, 64, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
            var img = wrtbmp.ToImage();
            // convert image into file stream.
            Stream filestram = img.ToStream();
            filestram.Position = 0;
            using (filestram)
            {
                // convert file stream into memory stream.
                var memoryStream = new MemoryStream();
                byte[] aryBuffer = new byte[16384];
                int nRead = filestram.Read(aryBuffer, 0, aryBuffer.Length);
                while (nRead > 0)
                {
                    memoryStream.Write(aryBuffer, 0, nRead);
                    nRead = filestram.Read(aryBuffer, 0, aryBuffer.Length);
                }
                // use following line to convert in bytes and save into database.
                memoryStream.ToArray();
                imgCustomImage.Source = CreateBitmapImage(memoryStream);
            }                        
        }
    }

    private BitmapImage CreateBitmapImage(MemoryStream memoryStream)
    {
        if ((memoryStream == null) || (memoryStream.Length == 0))
            return null;         

        var image = new BitmapImage();
        image.SetSource(memoryStream);
        return image;      
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文