从墨水画布保存图像

从墨水画布保存图像

本文介绍了从墨水画布保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从墨水画布中保存图像,但是保存后它不会创建任何文件,这是我的代码

I am trying to save an image from ink canvas however it does not create any file after saving, this is my code

RenderTargetBitmap rtb = new RenderTargetBitmap(
    (int)canvas.Width, (int)canvas.Height, 0, 0, PixelFormats.Default);

rtb.Render(this.canvas);

JpegBitmapEncoder encoder = new JpegBitmapEncoder();

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

using(var file = new FileStream(@"C:\test.jpg", FileMode.Create))
{
   encoder.Save(file);
}

但是,即使我更改目录,它也不会创建任何文件.没有异常被调用,也没有错误显示.该代码可以正常运行,没有任何问题,但是不存在要生成的文件.

however it does not create any file even when I change the directory. No exceptions is invoked and no errors are shown. The code just ran normally without any problem but the file meant to be generated is not there.

推荐答案

尝试使用保存文件"对话框

这是我在vb.net的类添加签名"中使用的示例代码

Here is an example code i used in my class "add signature" in vb.net

在按钮上单击保存文件",将出现一个对话框,输入文件名并按保存"后,它将另存为png图像(我使用了pngBitmap Encoder)

upon button click a savefile dialog box will appear, after putting a file name and pressing save, it will be save as a png image(i used pngBitmap Encoder)

与您使用的格式相同,但添加了保存文件对话框.

This has same format with what you are using but with a save file dialog added.

顺便说一句. WPFControl.Inkcanvas1是我的画布

btw. WPFControl.Inkcanvas1 is my inkcanvas

'buttonSaveAsClick


   'open save file dialog box
    Dim sfd As New SaveFileDialog()
    sfd.Filter = "Png Files(*.png)|*.png"

    'save file as png (render bitmap and convert/save to png)
    Dim result As Nullable(Of Boolean) = sfd.ShowDialog()
    Dim fileName As String = ""

    If result = True Then
        fileName = sfd.FileName

        Dim size As Size = New Point(750, 400) '= WPFControl.InkCanvas1.RenderSize
        Console.WriteLine(WPFControl.InkCanvas1.RenderSize)
        Dim rtb As New RenderTargetBitmap(CInt(size.Width), CInt(size.Height), 96, 96, Windows.Media.PixelFormats.Pbgra32)
        rtb.Render(WPFControl.InkCanvas1)
        Dim png As New PngBitmapEncoder()
        png.Frames.Add(BitmapFrame.Create(rtb))
        If String.IsNullOrEmpty(fileName) = True Then
            MsgBox("Please Enter a File Name", MsgBoxStyle.Exclamation, "File Name required!")
            Exit Sub

        Else
            Console.WriteLine(sfd.FileName)
            Console.WriteLine(convertImage.ConvertImageFiletoBytes(sfd.FileName))
        End If


        Using stm As Stream = File.Create(fileName)
            png.Save(stm)

        End Using
    End If

这篇关于从墨水画布保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:41