开发者

Scaling PNG images in silverlight

开发者 https://www.devze.com 2023-03-16 16:16 出处:网络
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

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 recen开发者_运维技巧tly. Any idea how can this be done in Silverlight?


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.


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;      
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消