How to save a “CroppedBitmap”

时光总嘲笑我的痴心妄想 提交于 2019-12-10 23:32:41

问题


I created a cropped Bitmap and now i want to save it. I know how to save a bitmapframe, but i dont know how to convert it to a bitmapframe.

Code:

' 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

So I want to save the new Image as file.


回答1:


Hi i did the same thing in C#, i hope you can convert into 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);
}



回答2:


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

}


来源:https://stackoverflow.com/questions/38281705/how-to-save-a-croppedbitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!