Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存图像_C#_Visual Studio_Visual Studio 2010_Windows Phone 7_Canvas - Fatal编程技术网

C# 保存图像

C# 保存图像,c#,visual-studio,visual-studio-2010,windows-phone-7,canvas,C#,Visual Studio,Visual Studio 2010,Windows Phone 7,Canvas,我正在为Windows Phone用C#制作一个绘图程序。 这是针对Windows Phone的,所以很多东西在C#中不起作用。 在打开.XAML页面的开始,我有一个空白画布。用户在画布上绘制,然后单击“保存”。当他/她单击Save时,我希望程序能够将图像保存在画布上。 到目前为止,我有以下代码: IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); StreamRe

我正在为Windows Phone用C#制作一个绘图程序。 这是针对Windows Phone的,所以很多东西在C#中不起作用。 在打开.XAML页面的开始,我有一个空白画布。用户在画布上绘制,然后单击“保存”。当他/她单击Save时,我希望程序能够将图像保存在画布上。 到目前为止,我有以下代码:

        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?

您的第一个部分似乎是将最后使用的数字写入
IsolatedStorage
中的文本文件,这似乎需要做很多工作才能完成相对简单的工作。您可以将整个部分替换为以下内容:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
int imageNumber = 0;
settings.TryGetValue<int>("PreviousImageNumber", out imageNumber);
imageNumber++;
但将图像保存到他们的
媒体库
可能更有意义,如下所示:

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 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);
settings["PreviousImageNumber"] = imageNumber;
settings.Save();
无论哪种方式,您都可以将
图像编号
保存回
隔离存储设置
,如下所示:

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 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);
settings["PreviousImageNumber"] = imageNumber;
settings.Save();

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

WriteableBitmap wbmp = new WriteableBitmap(yourCanvas, null);

文章还指出,画布的背景将被忽略并替换为黑色图像,但您可以通过首先向画布添加一个矩形来克服这一问题,该矩形具有您想要的任何背景。再说一次,我没试过这个。如果这不起作用,你应该考虑把另一个问题具体化为将画布转换成图像,因为这与你保存图像的原始问题实际上是一个独立的问题。

你的第一部分,你似乎在写一个文本文件中的最后一个使用的文件。做一些相对简单的事情需要做很多工作。您可以将整个部分替换为以下内容:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
int imageNumber = 0;
settings.TryGetValue<int>("PreviousImageNumber", out imageNumber);
imageNumber++;
但将图像保存到他们的
媒体库
可能更有意义,如下所示:

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 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);
settings["PreviousImageNumber"] = imageNumber;
settings.Save();
无论哪种方式,您都可以将
图像编号
保存回
隔离存储设置
,如下所示:

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 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);
settings["PreviousImageNumber"] = imageNumber;
settings.Save();

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

WriteableBitmap wbmp = new WriteableBitmap(yourCanvas, null);

文章还指出,画布的背景将被忽略并替换为黑色图像,但您可以通过首先向画布添加一个矩形来克服这一问题,该矩形具有您想要的任何背景。再说一次,我没试过这个。如果这不起作用,你应该考虑把另一个问题具体化为将画布转换成图像,因为这与你保存图像的原始问题实际上是一个独立的问题。

如果你可以使用CE保存可写图像,并且想知道如何保存画布,
然后,您需要将画布渲染为图像,或者如果您希望保存图形,则让用户直接绘制到图像,而不是画布,方法是将空白图像(带有alpha通道)放置在画布上,然后使用鼠标移动事件向图像添加笔刷标记。从而修改显示和未来输入保存方法。

如果您可以使用CE保存可写图像,并想知道如何保存画布,
然后,您需要将画布渲染为图像,或者如果您希望保存图形,则让用户直接绘制到图像,而不是画布,方法是将空白图像(带有alpha通道)放置在画布上,然后使用鼠标移动事件向图像添加笔刷标记。从而修改显示和未来输入以保存方法。

@Mitch:谢谢。现在添加一个问题…@Mitch:谢谢。现在添加一个问题…小修改:
string.format
应该是
string.format
@JavaAndCSharp我已经将代码更新为使用
string.format
,我只是直接在答案中键入了代码,但错过了那一个。这对您有用吗?string.Format有效(VS不会给我错误),但我不知道如何设置BitmapImage图像的值。可能是这样的:
image.SetSource(myPaintingCanvas)
@JavaAndCSharp
字符串。格式
语法正确。我已经用一个可能的解决方案更新了我的问题,用于从画布对象中提取图像,请让我知道它是否有效。效果非常好。谢谢总是惊讶于在这个网站上回答问题的速度和准确度。小小的修改:
string.format
应该是
string.format
@JavaAndCSharp我已经更新了代码以使用
string.format
,我只是直接在答案中键入代码,却错过了那一个。这对您有用吗?string.Format有效(VS不会给我错误),但我不知道如何设置BitmapImage图像的值。可能是这样的:
image.SetSource(myPaintingCanvas)
@JavaAndCSharp
字符串。格式
语法正确。我已经用一个可能的解决方案更新了我的问题,用于从画布对象中提取图像,请让我知道它是否有效。效果非常好。谢谢总是惊讶于如何快速和准确地回答这个网站上的问题。