Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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# 如何将字节数组转换为图像并在asp.net的图像控制器中显示图像_C#_Sql_Asp.net - Fatal编程技术网

C# 如何将字节数组转换为图像并在asp.net的图像控制器中显示图像

C# 如何将字节数组转换为图像并在asp.net的图像控制器中显示图像,c#,sql,asp.net,C#,Sql,Asp.net,我需要将字节数组转换为图像。我已将图像作为字节数组存储在sql server数据库中,该数据库是作为方法的参数获取的。 这是我的db表: Rid int Name varchar Email varchar Photo Image 试试这个 // Lets assume you have taken image byte array into imageBytes variable MemoryStream ms = new MemoryStream(imageBy

我需要将字节数组转换为图像。我已将图像作为字节数组存储在sql server数据库中,该数据库是作为方法的参数获取的。
这是我的db表:

Rid    int  
Name   varchar 
Email  varchar  
Photo  Image  
试试这个

// Lets assume you have taken image byte array into imageBytes variable
MemoryStream ms = new MemoryStream(imageBytes);
Image image= Image.FromStream(ms);

// Save the file into folder 
image.Save(filename: "filename with path");
试试这个

// Lets assume you have taken image byte array into imageBytes variable
MemoryStream ms = new MemoryStream(imageBytes);
Image image= Image.FromStream(ms);

// Save the file into folder 
image.Save(filename: "filename with path");
你可以使用并做类似的事情

Image img;

using (var ms = new MemoryStream(bytes))
{
    img= Image.FromStream(ms);
}
你可以使用并做类似的事情

Image img;

using (var ms = new MemoryStream(bytes))
{
    img= Image.FromStream(ms);
}

考虑到你最初的问题是

如何将字节数组转换为图像并在图像中显示图像 asp.net中的控制器

我假设您正在使用MVC。最简单的方法是像这样创建控制器

public class ImageController : Controller 
{
    public ActionResult GetImage(int i)
    {
        byte[] bytes = db.GetImage(i); //Get the image from your database
        return File(bytes, "image/png"); //or "image/jpeg", depending on the format
    }
}
然后,在您看来,只需使用

<img src="@Url.Action("GetImage", "Image", new { id = Model.Id })" />
在你的控制器里

public class YourController : Controller 
{
    public ActionResult YourView(int id)
    {
        var model = new YourModel();
        var image = db.GetImage(id);

        model.Id = id;
        model.ImageB64 = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(image));

        return View(model);
    }
}
在你看来,

<img src="@Model.ImageB64" />


但是像上面这样做不会缓存图像,并且会使CSS膨胀。通常,这不是显示图像的最佳方式。如果可能的话,坚持第一个选项。

考虑到你最初的问题是

如何将字节数组转换为图像并在图像中显示图像 asp.net中的控制器

我假设您正在使用MVC。最简单的方法是像这样创建控制器

public class ImageController : Controller 
{
    public ActionResult GetImage(int i)
    {
        byte[] bytes = db.GetImage(i); //Get the image from your database
        return File(bytes, "image/png"); //or "image/jpeg", depending on the format
    }
}
然后,在您看来,只需使用

<img src="@Url.Action("GetImage", "Image", new { id = Model.Id })" />
在你的控制器里

public class YourController : Controller 
{
    public ActionResult YourView(int id)
    {
        var model = new YourModel();
        var image = db.GetImage(id);

        model.Id = id;
        model.ImageB64 = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(image));

        return View(model);
    }
}
在你看来,

<img src="@Model.ImageB64" />


但是像上面这样做不会缓存图像,并且会使CSS膨胀。通常,这不是显示图像的最佳方式。如果可能,请坚持使用第一个选项。

是的,我尝试过。但它显示错误“system.web.ui.WebControl.image”不包含Fromstream的定义。您需要添加system.Drawing名称空间。删除图像的system.web.ui.webcontrols.image。使用System.Drawing命名空间中的图像类only@Abdu:问题解决了?@Jitendra Pancholi:还没有完全解决。我照你说的做了。但图像未显示。@Jitendra Pancholi:未完全解决。我会详细解释。我们都在安卓应用程序上工作。我的朋友在用Android做前端,我在用sql做asp.net的后端。我们在这个项目中使用Jsonservice(RESTFullWebService)。我们需要将图像存储到数据库中。在前端,我的朋友将图像转换为字节[],并通过方法作为参数发送。我刚刚将字节存储到名为photo的数据库字段中,其数据类型为image。但是我们找不到结果。是否有任何方法可以让我的朋友发送字节[]以外的图像是的,我尝试了此操作..但显示错误“system.web.ui.WebControl.image”不包含Fromstream的定义。您需要添加system.Drawing名称空间。删除图像的system.web.ui.webcontrols.image。使用System.Drawing命名空间中的图像类only@Abdu:问题解决了?@Jitendra Pancholi:还没有完全解决。我照你说的做了。但图像未显示。@Jitendra Pancholi:未完全解决。我会详细解释。我们都在安卓应用程序上工作。我的朋友在用Android做前端,我在用sql做asp.net的后端。我们在这个项目中使用Jsonservice(RESTFullWebService)。我们需要将图像存储到数据库中。在前端,我的朋友将图像转换为字节[],并通过方法作为参数发送。我刚刚将字节存储到名为photo的数据库字段中,其数据类型为image。但是我们找不到结果。有什么方法可以让我的朋友发送字节[]以外的图像吗