如何在ASP.NET中显示数据库中的WriteableBitmap [英] How to display WriteableBitmap from database in ASP.NET

查看:103
本文介绍了如何在ASP.NET中显示数据库中的WriteableBitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Silverlight中使用WriteablleBitmap将我的图像插入数据库。

将wrtieablebitmap转换为byte []:

I''m using WriteablleBitmap in Silverlight to insert my image into database.
Converting wrtieablebitmap to byte[]:

public byte[] ImageToByteArray(WriteableBitmap bitmap)
        {
            int[] p = bitmap.Pixels;
            int len = p.Length << 2;
            byte[] result = new byte[len];
            Buffer.BlockCopy(p, 0, result, 0, len);
            return result;
        }



然后,使用Silverlight WCF服务,我将其插入数据库:


Then, using Silverlight WCF Service, I''m inserting it to database:

[OperationContract]
        public void DoWork(byte[] img, string user, int id, double cen)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            produkty p = new produkty
            {
                zdjecie = img,
                userName = user,
                id_kategorii = id,
                cena = cen,
                opis = "opis"
            };
            db.produkties.InsertOnSubmit(p);
            try { db.SubmitChanges(); }
            catch (Exception e) { Console.WriteLine(e);  }
            return ;
        }



它应该是corect因为我可以在我的数据库中找到插入的数据。

接下来,我正在使用通用处理程序在default.aspx控件中显示来自数据库的图像 - 图像,但它不起作用。



处理程序源代码:


It should be corect becaouse i can find inserted data in my database.
Next, I''m using generic handler to display image from database in default.aspx in control - Image, but it isn''t working.

Handler source code:

string id = ctx.Request.QueryString["id"];
string conn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT zdjecie FROM produkty WHERE id_produktu=@id", con);
cmd.CommandType = CommandType.Text;
//cmd.Parameters.Add("@id", id);
cmd.Parameters.AddWithValue("@id", id);
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
ctx.Response.OutputStream.Write(pict, 78, pict.Length - 78);



数据库:

表:produkty

id_produktu - > int

zdjecie-> varbinary(max)

userName-> nvarchar(256)

id_kategorii-> int

cena>浮动

opis-> nvarchar(150)

我做错了什么?是否有必要转换什么?

对不起我的英文。


Database:
Table: produkty
id_produktu -> int
zdjecie->varbinary(max)
userName-> nvarchar(256)
id_kategorii->int
cena->float
opis->nvarchar(150)
What am I doing wrong? Does it necessary any convertion or something?
Sorry for my English.

推荐答案

你需要将你的字节数组改回一个Stream,如果您不想将其保存到硬盘,那么只需使用内存流。
You need to change your byte array back into a Stream, if you dont want to save it to the hard disk then just use a memory stream.


这篇关于如何在ASP.NET中显示数据库中的WriteableBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆