locked
如何存储PNG图片至模拟器的图片库 RRS feed

  • 问题

  • private void button1_Click(object sender, RoutedEventArgs e)
            {
                string tempJPEG = "WhiteParis.png";
                var store = IsolatedStorageFile.GetUserStoreForApplication();
                if (store.FileExists(tempJPEG))
                {
                    store.DeleteFile(tempJPEG);
                }

                IsolatedStorageFileStream fileStream = store.CreateFile(tempJPEG);
                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/WhiteParis.png", UriKind.Relative);
                sri = Application.GetResourceStream(uri);

                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wbmp = new WriteableBitmap(bitmap);

                Extensions.SaveJpeg(wbmp, fileStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 85);
                fileStream .Close ();


                fileStream = store.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);

                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("WhiteParis.png", fileStream);
                fileStream.Close();
            }

    只会保存JPG格式的,但是不知道PNG格式如何保存,别人说要下一个imageTools,但是刚刚接触这方面的开发,完全不会用- -求大神指点

    2012年5月15日 8:45

答案

  • 你好,我做了很多尝试,似乎MediaLibrary并不支持PNG的格式:

                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/girl.jpg", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("girl.jpg", sri.Stream);
                //以上代码是成功的(jpg)
    
    
                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/logo.png", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("logo.png", sri.Stream);
                //以上代码是失败的(png)

    所以我修改了代码如下,我在读取的地方做了手脚,把黑色的部分全部改成透明:

            <Grid DataContext="" x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
                <Button Content="Choose" Height="72" HorizontalAlignment="Left" Margin="12,88,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
                <Image Height="435" HorizontalAlignment="Left" Margin="12,166,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="438"></Image>
                <Image Height="339" HorizontalAlignment="Left" Margin="12,166,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="338"/>
            </Grid>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.IO.IsolatedStorage;
    using System.Windows.Resources;
    using System.Windows.Media.Imaging;
    using System.IO;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Phone.Tasks;
    
    namespace PhoneApp47
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                string tempJPEG = "logo.png";
                var store = IsolatedStorageFile.GetUserStoreForApplication();
                if (store.FileExists(tempJPEG))
                {
                    store.DeleteFile(tempJPEG);
                }
    
                IsolatedStorageFileStream fileStream = store.CreateFile(tempJPEG);
                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/logo.png", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
    
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wbmp = new WriteableBitmap(bitmap);
    
                Extensions.SaveJpeg(wbmp, fileStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 85);
                fileStream.Close();
    
    
                fileStream = store.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);
    
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("logo.png", fileStream);
                fileStream.Close();
    
    
            }
    
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                PhotoChooserTask task = new PhotoChooserTask();
                task.Completed += new EventHandler<PhotoResult>(task_Completed);
                task.Show();
            }
    
            void task_Completed(object sender, PhotoResult e)
            {
    
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                WriteableBitmap wb = new WriteableBitmap(bi);
                for (int i = 0; i < wb.Pixels.Count(); i++)
                {
                    if (wb.Pixels[i]<-16000000)
                        wb.Pixels[i] = 0;
                }
                image1.Source = wb;
                image2.Source = wb;
            }
        }
    }

    wb.Pixels[i]<-16000000就是改成透明的关键语句.

    希望可以帮到您

    • 已标记为答案 monkevin 2012年5月28日 5:30
    2012年5月24日 6:23
  • 你好,

    Image Tools可以从codeplex上下载,地址为:http://imagetools.codeplex.com/。

    关于它的使用指南,可以参考这个网页:http://imagetools.codeplex.com/wikipage?title=Write%20the%20content%20of%20a%20canvas%20to%20a%20file

    也可以参考stackoverflow上的这个帖子:Saving PNG image to Isolated Storage for WP7。


    Keep Fighting

    2012年5月16日 0:16

全部回复

  • 你好,

    Image Tools可以从codeplex上下载,地址为:http://imagetools.codeplex.com/。

    关于它的使用指南,可以参考这个网页:http://imagetools.codeplex.com/wikipage?title=Write%20the%20content%20of%20a%20canvas%20to%20a%20file

    也可以参考stackoverflow上的这个帖子:Saving PNG image to Isolated Storage for WP7。


    Keep Fighting

    2012年5月16日 0:16
  • 你好,Windows Phone 7 据我所知是支持Png图片的保存的:

    你可以查看一下以下代码,我logo.png图片设置为Content.

            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
                <Button Content="Choose" Height="72" HorizontalAlignment="Left" Margin="12,88,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
            </Grid>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.IO.IsolatedStorage;
    using System.Windows.Resources;
    using System.Windows.Media.Imaging;
    using System.IO;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Phone.Tasks;
    
    namespace PhoneApp47
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                string tempJPEG = "logo.png";
                var store = IsolatedStorageFile.GetUserStoreForApplication();
                if (store.FileExists(tempJPEG))
                {
                    store.DeleteFile(tempJPEG);
                }
    
                IsolatedStorageFileStream fileStream = store.CreateFile(tempJPEG);
                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/logo.png", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
    
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wbmp = new WriteableBitmap(bitmap);
    
                Extensions.SaveJpeg(wbmp, fileStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 85);
                fileStream.Close();
    
    
                fileStream = store.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);
    
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("logo.png", fileStream);
                fileStream.Close();
     
    
            }
    
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                PhotoChooserTask task = new PhotoChooserTask();
                task.Completed += new EventHandler<PhotoResult>(task_Completed);
                task.Show();
            }
    
            void task_Completed(object sender, PhotoResult e)
            {
            }
        }
    }

    我图片使用的是这张:

    http://krunchyworks.com/wp-content/uploads/2011/logos-png-format-i10.png

    2012年5月16日 2:18
  • 嗯,但是我对图片的要求是透明背景,这种方法好像是把PNG转换成JPEG格式存进去了,这样存储后图片就会有黑色背景了呀……怎么样才能直接保存无背景的PNG图片呢

    • 已编辑 monkevin 2012年5月16日 7:23
    2012年5月16日 7:17
  • I've tried this, however the reference for ImageTools.Util  and this example does not work. T.T
    2012年5月16日 7:35
  • 你好,我把储存进去的又读取出来,显示在Image控件里,成功读取了透明的数据,如图:

            void task_Completed(object sender, PhotoResult e)
            {
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                image1.Source = bi;
            }

    2012年5月17日 1:22
  • 您好,您看下我这个效果,我想实现个简单的试衣间功能,所以需要衣服是透明背景的,您看这有什么合适的解决办法吗?

    2012年5月18日 16:04
  • 你好,

    的确,会产生黑色底.

    你可以尝试下dearsj001的建议,dearsj001提供的帖子中,有个人已经解决了这个问题.

    2012年5月21日 5:15
  • 哎,那个人提供的参考帖子好像也没解决这个问题……看不大懂
    2012年5月23日 15:59
  • 你好,我做了很多尝试,似乎MediaLibrary并不支持PNG的格式:

                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/girl.jpg", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("girl.jpg", sri.Stream);
                //以上代码是成功的(jpg)
    
    
                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/logo.png", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("logo.png", sri.Stream);
                //以上代码是失败的(png)

    所以我修改了代码如下,我在读取的地方做了手脚,把黑色的部分全部改成透明:

            <Grid DataContext="" x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
                <Button Content="Choose" Height="72" HorizontalAlignment="Left" Margin="12,88,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
                <Image Height="435" HorizontalAlignment="Left" Margin="12,166,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="438"></Image>
                <Image Height="339" HorizontalAlignment="Left" Margin="12,166,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="338"/>
            </Grid>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.IO.IsolatedStorage;
    using System.Windows.Resources;
    using System.Windows.Media.Imaging;
    using System.IO;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Phone.Tasks;
    
    namespace PhoneApp47
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                string tempJPEG = "logo.png";
                var store = IsolatedStorageFile.GetUserStoreForApplication();
                if (store.FileExists(tempJPEG))
                {
                    store.DeleteFile(tempJPEG);
                }
    
                IsolatedStorageFileStream fileStream = store.CreateFile(tempJPEG);
                StreamResourceInfo sri = null;
                Uri uri = new Uri("Images/logo.png", UriKind.RelativeOrAbsolute);
                sri = Application.GetResourceStream(uri);
    
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wbmp = new WriteableBitmap(bitmap);
    
                Extensions.SaveJpeg(wbmp, fileStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 85);
                fileStream.Close();
    
    
                fileStream = store.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);
    
                MediaLibrary mediaLibrary = new MediaLibrary();
                Picture pic = mediaLibrary.SavePicture("logo.png", fileStream);
                fileStream.Close();
    
    
            }
    
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                PhotoChooserTask task = new PhotoChooserTask();
                task.Completed += new EventHandler<PhotoResult>(task_Completed);
                task.Show();
            }
    
            void task_Completed(object sender, PhotoResult e)
            {
    
                BitmapImage bi = new BitmapImage();
                bi.SetSource(e.ChosenPhoto);
                WriteableBitmap wb = new WriteableBitmap(bi);
                for (int i = 0; i < wb.Pixels.Count(); i++)
                {
                    if (wb.Pixels[i]<-16000000)
                        wb.Pixels[i] = 0;
                }
                image1.Source = wb;
                image2.Source = wb;
            }
        }
    }

    wb.Pixels[i]<-16000000就是改成透明的关键语句.

    希望可以帮到您

    • 已标记为答案 monkevin 2012年5月28日 5:30
    2012年5月24日 6:23