WPF将位图图像保存到浏览器 - 自托管Owin应用程序

时间:2015-09-17 23:53:20

标签: c# wpf owin self-hosting

我正在使用自托管WPF应用程序(使用Owin)将位图图像返回给浏览器。

我的控制器代码如下所示:

     public ImageSource GetmbTiles(string FILE, string Z, string X, string Y)
            {
                string mbtiles = string.Format(("C:\\{0}.mbtiles"), FILE);

                string connString = string.Format("Data Source={0}", mbtiles);

                using (SQLiteConnection conn = new SQLiteConnection(connString))
                {

                     System.Text.StringBuilder Query = new System.Text.StringBuilder();
                    Query.Append("SELECT tile_data ");
                    Query.Append("FROM tiles ");
                    Query.Append(string.Format("where zoom_level={0} and tile_column={1} and tile_row={2} ", Z, X, Y));
                    using (SQLiteCommand cmd = new SQLiteCommand(Query.ToString(), conn))
                    {

                        conn.Open();
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                buffer = GetBytes(dr);
                            }
                        }
                    }
                }

               //return new System.IO.MemoryStream(buffer);
                return byteArrayToImage(buffer);
            }

   private static BitmapImage byteArrayToImage(byte[] imageData)
        {
            if (imageData == null || imageData.Length == 0) return null;
            var image = new BitmapImage();
            using (var mem = new MemoryStream(imageData))
            {
                mem.Position = 0;
                image.BeginInit();
                image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.UriSource = null;
                image.StreamSource = mem;
                image.EndInit();
            }
            image.Freeze();
            return image;
        }


        private static byte[] GetBytes(SQLiteDataReader reader)
        {
            const int CHUNK_SIZE = 2 * 1024;
            byte[] buffer = new byte[CHUNK_SIZE];
            long bytesRead = 0;
            long fieldOffset = 0;
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length);
                while (bytesRead == buffer.Length)
                {
                    stream.Write(buffer, 0, Convert.ToInt32(bytesRead));
                    fieldOffset += bytesRead;
                    bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length);
                }

                stream.Write(buffer, 0, Convert.ToInt32(bytesRead));

                return stream.ToArray();
            }
        }

当我从浏览器调用此控制器时,我只得到一个json文件,而不是图像。

例外消息

<ExceptionMessage>Type 'System.IO.MemoryStream' with data contract name 'MemoryStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.</ExceptionMessage>

我是Web开发人员对WPF世界的新手,请原谅这个noob问题。但是到目前为止我试图返回的图像都无济于事。我可以看到正在填充字节数组。但字节数组不会在浏览器中显示,是否有一些我失踪的转换?任何暗示或帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

我正在为可能处于同一场景的其他任何人发布此解决方案,OWIN Self Hosted App提供图像或其他文件。

我通过将其添加到控制器方法

解决了这个问题
  Image img = byteArrayToImage(buffer);
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(ms.ToArray());
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
            return result;



  public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }