代码之家  ›  专栏  ›  技术社区  ›  Greg Finzer

BitmapImage的to-FromFile和ToFile等价物是什么?

  •  1
  • Greg Finzer  · 技术社区  · 14 年前

    System.Drawing.Image具有用于FromFile和ToFile的易于使用的方法。Silverlight BitmapImage的等价物是什么?我正在尝试加载和保存一个jpeg图像作为单元测试的一部分。字节必须匹配 确切地 让它过去。以下是我目前的猜测:

        //I am not sure this is right
        private BitmapImage GetImage(string fileName)
        {
            BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
            using (Stream imageStreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = imageStreamSource;
                bitmapImage.EndInit();
            }
    
            return bitmapImage;
        }
    
        private void SaveImage(BitmapImage bitmapImage, string file)
        {
            //How to do this?
        }
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   AnthonyWJones    14 年前

    浏览器内的Silverlight应用程序无法使用文件名打开文件。要做到这一点,你需要它在浏览器中以更高的信任度运行。

    Silverlight没有内置的图像编码,因此您无法获取位图的内容(顺便说一句,您需要使用 WriteableBitmap 能够访问原始图像)。

    Image Tools .

        2
  •  1
  •   Gishu    14 年前

    来自MSDN link

    BitmapImage myBitmapImage = new BitmapImage();
    
    // BitmapImage.UriSource must be in a BeginInit/EndInit block
    myBitmapImage.BeginInit();
    myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");
    myBitmapImage.DecodePixelWidth = 200;
    myBitmapImage.EndInit();
    
    //set image source
    myImage.Source = myBitmapImage;
    

    要将图像保存到磁盘,您需要特定的编码器。 JpegBitmapEncoder