C# 从墨水画布保存图像
我试图从ink canvas保存图像,但保存后它不会创建任何文件,这是我的代码C# 从墨水画布保存图像,c#,filestream,inkcanvas,jpegbitmapencoder,C#,Filestream,Inkcanvas,Jpegbitmapencoder,我试图从ink canvas保存图像,但保存后它不会创建任何文件,这是我的代码 RenderTargetBitmap rtb = new RenderTargetBitmap( (int)canvas.Width, (int)canvas.Height, 0, 0, PixelFormats.Default); rtb.Render(this.canvas); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.
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);
}
但是,即使我更改了目录,它也不会创建任何文件。不会调用任何异常,也不会显示任何错误。代码刚刚正常运行,没有任何问题,但要生成的文件不在那里。尝试使用保存文件对话框 下面是我在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
尝试使用保存文件对话框 下面是我在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
使用Microsoft.Win32;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Security.AccessControl;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfInkCavasSaveImage
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
inkCanvas1.Width=System.Windows.SystemParameters.WorkArea.Width;
inkCanvas1.Height=System.Windows.SystemParameters.WorkArea.Height;
}
私有void btnSaveInkCanvas(对象发送方,路由目标)
{
string subpath=Directory.GetCurrentDirectory();
SaveFileDialog saveFileDialog12=新建SaveFileDialog();
saveFileDialog12.Filter=“JPeg图像|*.jpg |位图图像|*.bmp | Gif图像|*.Gif | Png文件|*.Png”;
saveFileDialog12.Title=“保存图像文件”;
saveFileDialog12.InitialDirectory=子路径;
saveFileDialog12.ShowDialog();
if(saveFileDialog12.FileName==“”)返回;
subpath=saveFileDialog12.FileName.Substring(0,saveFileDialog12.FileName.Length-saveFileDialog12.SafeFileName.Length);
RenderTargetBitmap rtb=新的RenderTargetBitmap((int)inkCanvas1.Width,(int)inkCanvas1.Height,96d,96d,PixelFormats.Default);
rtb.渲染(inkCanvas1);
DrawingVisual dvInk=新建DrawingVisual();
DrawingContext dcInk=dvInk.renderropen();
DrawRectangle(inkCanvas1.Background,null,new Rect(0d,0d,inkCanvas1.Width,inkCanvas1.Height));
foreach(inkCanvas1.Strokes中的System.Windows.Ink.Stroke笔划)
{
笔画(dcInk);
}
dcInk.Close();
FileStream fs=File.Open(saveFileDialog12.FileName,FileMode.OpenOrCreate);//将位图保存到文件
System.Windows.Media.Imaging.JpegBitmapEncoder编码器1=新的JpegBitmapEncoder();
encoder1.Frames.Add(BitmapFrame.Create(rtb));
编码器1.Save(fs);
fs.Close();
}
}
}
使用Microsoft.Win32;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Security.AccessControl;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfInkCavasSaveImage
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
inkCanvas1.Width=System.Windows.SystemParameters.WorkArea.Width;
inkCanvas1.Height=System.Windows.SystemParameters.WorkArea.Height;
}
私有void btnSaveInkCanvas(对象发送方,路由目标)
{
string subpath=Directory.GetCurrentDirectory();
SaveFileDialog saveFileDialog12=新建SaveFileDialog();
saveFileDialog12.Filter=“JPeg图像|*.jpg |位图图像|*.bmp | Gif图像|*.Gif | Png文件|*.Png”;
saveFileDialog12.Title=“保存图像文件”;
saveFileDialog12.InitialDirectory=子路径;
saveFileDialog12.ShowDialog();
if(saveFileDialog12.FileName==“”)返回;
subpath=saveFileDialog12.FileName.Substring(0,saveFileDialog12.FileName.Length-saveFileDialog12.SafeFileName.Length);
RenderTargetBitmap rtb=新的RenderTargetBitmap((int)inkCanvas1.Width,(int)inkCanvas1.Height,96d,96d,PixelFormats.Default);
rtb.渲染(inkCanvas1);
DrawingVisual dvInk=新建DrawingVisual();
DrawingContext dcInk=dvInk.renderropen();
DrawRectangle(inkCanvas1.Background,null,new Rect(0d,0d,inkCanvas1.Width,inkCanvas1.Height));
foreach(System.Windows.Ink.stro)