保存图像

发布于 2024-11-07 19:46:13 字数 1960 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

時窥 2024-11-14 19:46:13

您似乎将最后使用的数字写入 IsolatedStorage 中的文本文件的第一部分似乎需要大量工作来完成相对简单的事情。您可以用以下内容替换整个部分:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
int imageNumber = 0;
settings.TryGetValue<int>("PreviousImageNumber", out imageNumber);
imageNumber++;

您可以将图像保存到 IsolatedStorage,如下所示:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!isf.DirectoryExists("Images"))
    {
        isf.CreateDirectory("Images");
    }
    IsolatedStorageFileStream fstream = isf.CreateFile(string.Format("Images\\{0}.jpg",imageNumber));

    WriteableBitmap wbmp = new WriteableBitmap(image);
    Extensions.SaveJpeg(wbmp, fstream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
    fstream.Close();
}

但将图像保存到其 MediaLibrary 可能更有意义,如下所示:

MediaLibrary library = new MediaLibrary();
WriteableBitmap wbmp = new WriteableBitmap(image);

MemoryStream ms = new MemoryStream();
Extensions.SaveJpeg(wbmp, ms, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
ms.Seek(0, SeekOrigin.Begin);
library.SavePicture(string.Format("Images\\{0}.jpg",imageNumber), ms);

无论哪种方式,您都可以将imageNumber保存回IsolatedStorageSettings,如下所示:

settings["PreviousImageNumber"] = imageNumber;
settings.Save();

我假设上面使用的image设置在您的其他位置代码。我以前没有将画布保存到图像中,但一些快速搜索发现了这个 blog 其中使用 WriteableBitmap 给出了一个示例,表明您可以将图像变量替换为画布元素:

WriteableBitmap wbmp = new WriteableBitmap(yourCanvas, null);

该文章还指出画布的背景将被忽略并替换为黑色图像,但您可以通过首先向画布添加一个具有所需背景的矩形来克服此问题。再说一遍,我还没有尝试过。如果这不起作用,您应该考虑发布另一个与将画布转换为图像专门相关的问题,因为这实际上是与保存图像的原始问题不同的问题。

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:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
int imageNumber = 0;
settings.TryGetValue<int>("PreviousImageNumber", out imageNumber);
imageNumber++;

You can save the image to IsolatedStorage like this:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!isf.DirectoryExists("Images"))
    {
        isf.CreateDirectory("Images");
    }
    IsolatedStorageFileStream fstream = isf.CreateFile(string.Format("Images\\{0}.jpg",imageNumber));

    WriteableBitmap wbmp = new WriteableBitmap(image);
    Extensions.SaveJpeg(wbmp, fstream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
    fstream.Close();
}

But it would probably make more sense to save the image to their MediaLibrary like this:

MediaLibrary library = new MediaLibrary();
WriteableBitmap wbmp = new WriteableBitmap(image);

MemoryStream ms = new MemoryStream();
Extensions.SaveJpeg(wbmp, ms, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
ms.Seek(0, SeekOrigin.Begin);
library.SavePicture(string.Format("Images\\{0}.jpg",imageNumber), ms);

Either way, you can then save the imageNumber back to the IsolatedStorageSettings like this:

settings["PreviousImageNumber"] = imageNumber;
settings.Save();

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:

WriteableBitmap wbmp = new WriteableBitmap(yourCanvas, null);

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.

决绝 2024-11-14 19:46:13

如果您可以使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文