保存图像
我正在使用 C# for Windows Phone 制作一个绘图程序。
这是针对 Windows Phone 的,因此很多在 C# 中可以工作的东西不起作用。
在开始打开 .XAML 页面时,我有一个空白的画布。用户在画布上绘图,然后单击“保存”。当他/她单击“保存”时,我希望程序能够将图像保存在画布上。
到目前为止,我有以下代码:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader sr = null;
sr = new StreamReader(new IsolatedStorageFileStream("Data\\imagenum.txt", FileMode.Open, isf));
test = sr.ReadLine();
sr.Close();
int.TryParse(test, out test2);
test2 = test2 + 1;
IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForApplication();
isf2.CreateDirectory("Data");
StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Data\\imagenum.txt", FileMode.Create, isf2));
sw.WriteLine(test2);
//This writes the content of textBox1 to the StreamWriter. The StreamWriter writes the text to the file.
sw.Close();
该代码查找图像的合适名称。
我还在网上找到了各种其他代码片段:
// Construct a bitmap from the button image resource.
test = "Images/" + test + ".jpg";
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
WriteableBitmap bmImage = new WriteableBitmap(image);
if (!store.DirectoryExists("Images"))
{
store.CreateDirectory("Images");
}
using (IsolatedStorageFileStream isoStream =
store.OpenFile(@"Images\" + test + ".jpg", FileMode.OpenOrCreate))
{
Extensions.SaveJpeg(
bmImage,
isoStream,
bmImage.PixelWidth,
bmImage.PixelHeight,
0,
100);
}
}
上面是来自 MSDN 教程和我自己拼凑在一起的代码的丑陋混乱的代码。
(由于半明显的原因,它不起作用)
如何将画布作为图像保存到isolatedStorage?
I'm making a drawing program in C# for Windows Phone.
This is for Windows Phone, so a bunch of stuff doesn't work that would work in C#.
At the start of opening a .XAML page, I have a blank Canvas. The user draws on the Canvas, then clicks Save. When he/she clicks Save, I want the program to be able to save the image on the Canvas.
I have the following code so far:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader sr = null;
sr = new StreamReader(new IsolatedStorageFileStream("Data\\imagenum.txt", FileMode.Open, isf));
test = sr.ReadLine();
sr.Close();
int.TryParse(test, out test2);
test2 = test2 + 1;
IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForApplication();
isf2.CreateDirectory("Data");
StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Data\\imagenum.txt", FileMode.Create, isf2));
sw.WriteLine(test2);
//This writes the content of textBox1 to the StreamWriter. The StreamWriter writes the text to the file.
sw.Close();
This code finds what an appropriate name for the image would be.
I've also found various other code snippets on the web:
// Construct a bitmap from the button image resource.
test = "Images/" + test + ".jpg";
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
WriteableBitmap bmImage = new WriteableBitmap(image);
if (!store.DirectoryExists("Images"))
{
store.CreateDirectory("Images");
}
using (IsolatedStorageFileStream isoStream =
store.OpenFile(@"Images\" + test + ".jpg", FileMode.OpenOrCreate))
{
Extensions.SaveJpeg(
bmImage,
isoStream,
bmImage.PixelWidth,
bmImage.PixelHeight,
0,
100);
}
}
The above is an ugly mess of code from tutorials on MSDN and my own badly scraped together code.
(It doesn't work, for semi-obvious reasons)
How would I save the canvas to IsolatedStorage, as an image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎将最后使用的数字写入
IsolatedStorage
中的文本文件的第一部分似乎需要大量工作来完成相对简单的事情。您可以用以下内容替换整个部分:您可以将图像保存到
IsolatedStorage
,如下所示:但将图像保存到其
MediaLibrary
可能更有意义,如下所示:无论哪种方式,您都可以将
imageNumber
保存回IsolatedStorageSettings
,如下所示:我假设上面使用的
image
设置在您的其他位置代码。我以前没有将画布保存到图像中,但一些快速搜索发现了这个 blog 其中使用 WriteableBitmap 给出了一个示例,表明您可以将图像变量替换为画布元素:该文章还指出画布的背景将被忽略并替换为黑色图像,但您可以通过首先向画布添加一个具有所需背景的矩形来克服此问题。再说一遍,我还没有尝试过。如果这不起作用,您应该考虑发布另一个与将画布转换为图像专门相关的问题,因为这实际上是与保存图像的原始问题不同的问题。
Your first section where you appear to be writing the last used number to a text file in
IsolatedStorage
seems like a lot of work to do something relatively simple. You can replace that whole section with this:You can save the image to
IsolatedStorage
like this:But it would probably make more sense to save the image to their
MediaLibrary
like this:Either way, you can then save the
imageNumber
back to theIsolatedStorageSettings
like this:I had assumed the
image
used above was set somewhere else in your code. I haven't saved a Canvas to an image before, but some quick searching turned up this blog where an example is given using the WriteableBitmap which indicates you can just replace the image variable with your canvas element:The article also indicates that the Canvas' background will be ignored and replaced with a black image but that you can overcome this by first adding a rectangle to the Canvas with whatever background you want. Again, I haven't tried this. If this doesn't work you should consider posting another question specifically related to transforming a Canvas to an Image since this is really a separate issue from your original question about saving images.
如果您可以使用CE保存可写图像并想知道如何保存画布,
那么您需要将画布渲染为图像,或者如果您想保存绘图,则让用户直接绘制到图像,而不是画布,方法是将空白图像(带有 Alpha 通道)放在画布上,然后使用鼠标移动事件添加画笔标记图像。从而修改显示和将来的输入以保存方法。
If you can save the writeable image using CE and want to know how to save the canvas,
then you need to either render canvas to image or if you want to save drawing, then have user draw directly to image, not canvas, by putting blank image (with alpha channel) on canvas, then using mouse move event to add brush marks to image. Thus modifying both display and future input to save method.