如何在WPF VB中将croppedbitmap转换为bitmapimage [英] How to convert croppedbitmap to bitmapimage in WPF VB

查看:96
本文介绍了如何在WPF VB中将croppedbitmap转换为bitmapimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助..





提前致谢。



我尝试了什么:



i不知道如何将CroppedBitmap转换为BitmapImage

please Help..


Thanks in advance.

What I have tried:

i don't know how to convert CroppedBitmap to BitmapImage

推荐答案

如果你看一下上一个问题的答案:如何在WPF VB中裁剪图像 [ ^ ],你会看到这一行:

If you look at the answer to the previous question that you asked: HOW to crop image in WPF VB[^], you would have seen this line:
var img = image1.Source as BitmapSource;



然后这个:


and then this:

image2.Source = new CroppedBitmap(inputImage, rcFrom);



并根据 Microsoft文档[ ^ ],也在上一个问题的答案中链接,有以下评论:


and according to Microsoft documentation[^], also linked in the previous question's answers, has the following comment:

Quote:

以下示例使用CroppedBitmap作为源来创建图像。

The following example creates an image using a CroppedBitmap as its source.



因此,CroppedBitmap实际上返回 BitmapSource 类型的位图对象,其中包含您在上述问题中要求的更多信息:BitmapSource类 - Microsoft文档 [ ^ ]及其示例。



更新#1



在上面扩展( CroppedBitmap.Source 返回 BitmapSource 类型),并使用您之前的问题的解决方案,以及谷歌搜索&使用找到的文章 c# - BitmapSource to BitmapImage - Stack Overflow [ ^ ],如果你做了一点点工作,答案就在那里,就像我刚才那样。



这是一个工作解决方案,可让您选择图像编码(Jpg或Png):


Therefore, CroppedBitmap actually returns a bitmapped object of BitmapSource type which has more information that you are asking for in the question above: BitmapSource Class - Microsoft documentation[^] with examples.

UPDATE #1

Expanding on above (CroppedBitmap.Source returns a BitmapSource type), and using the solution to your previous question, along with a google search & using the found article c# - BitmapSource to BitmapImage - Stack Overflow[^], the answer was there if you did a little work, like I just did.

Here is a working solution that gives you the choice of image encoding (Jpg or Png):

private void Go_Click(object sender, RoutedEventArgs e)
{
    if (image1.Source != null)
    {
        var rect1 = new Rect()
        {
            X = Canvas.GetLeft(selectionRectangle),
            Y = Canvas.GetTop(selectionRectangle),
            Width = selectionRectangle.Width,
            Height = selectionRectangle.Height
        };

        // calc scale in PIXEls for CroppedBitmap...
        var img = image1.Source as BitmapSource;
        var scaleWidth = (img.PixelWidth) / (image1.ActualWidth);
        var scaleHeight = (img.PixelHeight) / (image1.ActualHeight);

        var rcFrom = new Int32Rect()
        {
            X = (int)(rect1.X * scaleWidth),
            Y = (int)(rect1.Y * scaleHeight),
            Width = (int)(rect1.Width * scaleWidth),
            Height = (int)(rect1.Height * scaleHeight)
        };

        var cropped = new CroppedBitmap(inputImage, rcFrom);

        // UPDATE HERE: CroppedBitmap to BitmapImage
        croppedImage = GetJpgImage(cropped.Source);
        // or 
        // croppedImage = GetPngImage(cropped.Source);

        // using BitmapImage version to prove its created successfully
        image2.Source = croppedImage; //cropped;
    }
}

private BitmapImage croppedImage;

private BitmapImage GetJpgImage(BitmapSource source)
{
    return GetImage(source, new JpegBitmapEncoder());
}

private BitmapImage GetPngImage(BitmapSource source)
{
    return GetImage(source, new PngBitmapEncoder());
}

private BitmapImage GetImage(BitmapSource source, BitmapEncoder encoder)
{
    var bmpImage = new BitmapImage();

    using (var srcMS = new MemoryStream())
    {
        encoder.Frames.Add(BitmapFrame.Create(source));
        encoder.Save(srcMS);

        srcMS.Position = 0;
        using (var destMS = new MemoryStream(srcMS.ToArray()))
        {
            bmpImage.BeginInit();
            bmpImage.StreamSource = destMS;
            bmpImage.CacheOption = BitmapCacheOption.OnLoad;
            bmpImage.EndInit();
            bmpImage.Freeze();
        }
    }

    return bmpImage;
}





更新#2 如下所述,以下是如何保存...



UPDATE #2 As mentioned below, here is how to save...

private void Save_Click(object sender, RoutedEventArgs e)
{
    // save to current working directory
    SaveJpgImage(croppedImage, @".\cropped.jpg");

}

private void SaveJpgImage(BitmapImage source, string photoLocation)
{
    SaveImage(source, photoLocation, new JpegBitmapEncoder());
}

private void SavePngImage(BitmapImage source, string photoLocation)
{
    SaveImage(source, photoLocation, new PngBitmapEncoder());
}

private void SaveImage(BitmapImage source, string photoLocation, BitmapEncoder encoder)
{
    encoder.Frames.Add(BitmapFrame.Create(source));

    using (var filestream = new FileStream(photoLocation, FileMode.Create))
        encoder.Save(filestream);
}





更新#3



刚注意到VB标签 - 抱歉...下面的VB版本......



UPDATE #3

Just noticed the VB tag - sorry about that... VB version below...

Imports System.IO

Class MainWindow

    Private isDragging As Boolean
    Private anchorPoint As New Point()

    Public Sub New()

        InitializeComponent()

        inputImage = New BitmapImage(New Uri("pack://application:,,,/Images/Code Project Sample_300 dpi.JPEG"))
        'inputImage = new BitmapImage(new Uri("http://eskipaper.com/images/cool-morning-fog-wallpaper-1.jpg"));
        image1.Source = inputImage

        Go.IsEnabled = False
        image2.Source = Nothing

    End Sub

    Private ReadOnly Property inputImage As BitmapImage

    Private Sub Go_Click(sender As Object, e As RoutedEventArgs)

        If image1.Source IsNot Nothing Then

            Dim rect1 = New Rect() With {
                    .X = Canvas.GetLeft(selectionRectangle),
                    .Y = Canvas.GetTop(selectionRectangle),
                    .Width = selectionRectangle.Width,
                    .Height = selectionRectangle.Height
                }

            ' calc scale in PIXEls for CroppedBitmap...
            Dim img = TryCast(image1.Source, BitmapSource)
            Dim scaleWidth = (img.PixelWidth) / (image1.ActualWidth)
            Dim scaleHeight = (img.PixelHeight) / (image1.ActualHeight)

            Dim rcFrom = New Int32Rect() With {
                    .X = CInt(rect1.X * scaleWidth),
                    .Y = CInt(rect1.Y * scaleHeight),
                    .Width = CInt(rect1.Width * scaleWidth),
                    .Height = CInt(rect1.Height * scaleHeight)
                }

            Dim cropped = New CroppedBitmap(inputImage, rcFrom)
            croppedImage = GetJpgImage(cropped)

            ' using BitmapImage version to prove its created successfully
            image2.Source = croppedImage 'cropped

        End If

    End Sub

    Private croppedImage As BitmapImage

    Private Function GetJpgImage(source As BitmapSource) As BitmapImage

        Return GetImage(source, New JpegBitmapEncoder())

    End Function

    Private Function GetPngImage(source As BitmapSource) As BitmapImage

        Return GetImage(source, New PngBitmapEncoder())

    End Function

    Private Function GetImage(source As BitmapSource, encoder As BitmapEncoder) As BitmapImage

        Dim bmpImage = New BitmapImage()

        Using srcMS = New MemoryStream()

            encoder.Frames.Add(BitmapFrame.Create(source))
            encoder.Save(srcMS)

            srcMS.Position = 0
            Using destMS = New MemoryStream(srcMS.ToArray())
                bmpImage.BeginInit()
                bmpImage.StreamSource = destMS
                bmpImage.CacheOption = BitmapCacheOption.OnLoad
                bmpImage.EndInit()
                bmpImage.Freeze()
            End Using

        End Using

        Return bmpImage

    End Function

#Region "Mouse events"
 
    Private Sub image1_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)

        If Not isDragging Then
            anchorPoint.X = e.GetPosition(BackPanel).X
            anchorPoint.Y = e.GetPosition(BackPanel).Y
            isDragging = True
        End If

    End Sub

    Private Sub image1_MouseMove(sender As Object, e As MouseEventArgs)

        If isDragging Then

            Dim x As Double = e.GetPosition(BackPanel).X
            Dim y As Double = e.GetPosition(BackPanel).Y

            selectionRectangle.SetValue(Canvas.LeftProperty, Math.Min(x, anchorPoint.X))
            selectionRectangle.SetValue(Canvas.TopProperty, Math.Min(y, anchorPoint.Y))
            selectionRectangle.Width = Math.Abs(x - anchorPoint.X)
            selectionRectangle.Height = Math.Abs(y - anchorPoint.Y)

            If selectionRectangle.Visibility <> Visibility.Visible Then
                selectionRectangle.Visibility = Visibility.Visible
            End If

        End If

    End Sub

    Private Sub image1_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)

        If isDragging Then

            isDragging = False

            If selectionRectangle.Width > 0 Then
                Go.Visibility = Visibility.Visible
                Go.IsEnabled = True
            End If

            If selectionRectangle.Visibility <> Visibility.Visible Then
                selectionRectangle.Visibility = Visibility.Visible
            End If

        End If

    End Sub

    Private Sub RestRect()

        selectionRectangle.Visibility = Visibility.Collapsed
        isDragging = False

    End Sub

#End Region

    Private Sub Save_Click(sender As Object, e As RoutedEventArgs)

        ' save to current working directory
        SaveJpgImage(croppedImage, ".\cropped.jpg")

    End Sub

    Private Sub SaveJpgImage(source As BitmapImage, photoLocation As String)

        SaveImage(source, photoLocation, New JpegBitmapEncoder())

    End Sub

    Private Sub SavePngImage(source As BitmapImage, photoLocation As String)

        SaveImage(source, photoLocation, New PngBitmapEncoder())

    End Sub

    Private Sub SaveImage(source As BitmapImage, photoLocation As String, encoder As BitmapEncoder)

        encoder.Frames.Add(BitmapFrame.Create(source))

        Using filestream = New FileStream(photoLocation, FileMode.Create)
            encoder.Save(filestream)
        End Using

    End Sub

End Class


这篇关于如何在WPF VB中将croppedbitmap转换为bitmapimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆