如何保存“CroppedBitmap”

时间:2016-07-09 12:13:36

标签: wpf vb.net save bitmapimage

我创建了一个裁剪的Bitmap,现在我想保存它。我知道如何保存位图帧,但我不知道如何将其转换为位图帧。

代码:

' Create an Image element.
    Dim croppedImage As New Image()
    croppedImage.Width = 200
    croppedImage.Margin = New Thickness(5)

    ' Create a CroppedBitmap based off of a xaml defined resource.
    Dim cb As New CroppedBitmap(CType(screenshot, BitmapSource), New Int32Rect(X, Y, breite, höhe))
    'select region rect
    croppedImage.Source = cb 'set image source to cropped

所以我想将新图像保存为文件。

2 个答案:

答案 0 :(得分:0)

您好我在C#中做了同样的事情,我希望您可以转换为vb

void saveCroppedBitmap(CroppedBitmap image,string path)
{
    FileStream mStream = new FileStream(path, FileMode.Create);
    JpegBitmapEncoder jEncoder = new JpegBitmapEncoder();
    jEncoder.Frames.Add(BitmapFrame.Create(image)); 
    jEncoder.Save(mStream);
}

答案 1 :(得分:-1)

private void CropBitmap(BitmapFrame bitmapFrame,int height,string filename,BitmapEncoder encoder)
{
    CroppedBitmap croppedBitmap = new CroppedBitmap(bitmapFrame, new Int32Rect(0,0,(int)bitmapFrame.Width, height));

    encoder.Frames.Add(BitmapFrame.Create(croppedBitmap));

    using (System.IO.FileStream fs = File.Create(filename))
    {
        encoder.Save(fs);
    }

}