从墨水画布中保存图像

时间:2013-07-30 03:32:42

标签: c# filestream inkcanvas jpegbitmapencoder

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

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

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

2 个答案:

答案 0 :(得分:0)

尝试使用保存文件对话框

以下是我在班级中使用的示例代码"添加签名"在vb.net

点击按钮后会出现一个保存文件对话框,输入文件名后按保存,它将保存为png图像(我使用的是pngBitmap编码器)

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

顺便说一句。 WPFControl.Inkcanvas1是我的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

答案 1 :(得分:0)

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/gh/nespero/fluent-kit@1.2.0/css/fluent-kit.min.css" rel="stylesheet"/>

<table class="table table-responsive">
  <caption>Last week statistics</caption>
  <thead>
  <tr>
    <th>Make</th>
    <th>Model</th>
    <th>Origin</th>
    <th>Producent</th>
    <th>Doors</th>
    <th>Engine</th>
    <th>Petrol</th>
    <th>First release</th>
    <th>Price 2016</th>
    <th>Price 2017</th>
    <th>Maximum speed</th>
    <th>Gears</th>
    <th>Manufactured</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>Audi</td>
    <td>R8</td>
    <td>Germany</td>
    <td>Audi</td>
    <td>3</td>
    <td>R8 V8 4.2 FSI quattro</td>
    <td>Gasoline</td>
    <td>2006</td>
    <td>$1.000.000</td>
    <td>$950.000</td>
    <td>375 km /h</td>
    <td>7</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Audi</td>
    <td>R8</td>
    <td>Germany</td>
    <td>Audi</td>
    <td>3</td>
    <td>R8 V8 4.2 FSI quattro</td>
    <td>Gasoline</td>
    <td>2006</td>
    <td>$1.000.000</td>
    <td>$950.000</td>
    <td>375 km /h</td>
    <td>7</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Audi</td>
    <td>R8</td>
    <td>Germany</td>
    <td>Audi</td>
    <td>3</td>
    <td>R8 V8 4.2 FSI quattro</td>
    <td>Gasoline</td>
    <td>2006</td>
    <td>$1.000.000</td>
    <td>$950.000</td>
    <td>375 km /h</td>
    <td>7</td>
    <td>Germany</td>
  </tr>
  </tbody>
</table>