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);
and according to
Microsoft documentation[
^], also linked in the previous question's answers, has the following comment:
Quote:
The following example creates an image using a CroppedBitmap as its source.
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
};
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);
croppedImage = GetJpgImage(cropped.Source);
image2.Source = croppedImage;
}
}
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;
}
UPDATE #2 As mentioned below, here is how to save...
private void Save_Click(object sender, RoutedEventArgs e)
{
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);
}
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"))
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
}
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)
image2.Source = croppedImage
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)
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