c# - 如何在asp.net c#中返回图像方法的图像字节
问题描述
protected void Register_Click(object sender, EventArgs e)
{
Properties.username = UN_TextBox.Text;
Properties.Email = Email_TextBox.Text;
Properties.Pass = Pass_TextBox.Text;
Properties.Gender = Gen_RadioButtonList.Text;
Properties.Img = img();
int a = UserAccessLogic.Signup();
if (a > 0)
{
Msg_Label.Text = ("Registration successful..!");
} else {
Msg_Label.Text = ("Registration fail..!");
}
}
private byte[] img()
{
string path = Server.MapPath("Picture/");
if (FileUpload.HasFile)
{
string fname = FileUpload.FileName;
string extension = Path.GetExtension(fname);
if (extension.ToLower() == ".jpg" || extension.ToLower() == ".png" || extension.ToLower() == ".jpeg")
{
FileUpload.SaveAs(Server.MapPath("Images/" + fname));
//how to return this line of code to byte[] img() method
}
}
}
解决方案
您可以将 Image 读取为字节数组:
string path=Server.MapPath("Images/" + fname);
byte[] imgdata = System.IO.File.ReadAllBytes(path);
return imgdata;
你也可以使用ImageConverter
ImageConverter ic = new ImageConverter();
byte[] imgdata = (byte[])ic.ConvertTo(img, typeof(byte[]));
return imgdata ;
推荐阅读
- java - Android 视图绑定。如何在 Basic Activity/Fragment 中实现绑定?
- php - PHP无法使用DOM函数获取url链接内容
- php - 如何从 PHP 中的字符串中获取 n 个单词的组合
- python - 如何在 Python Django 中从表单实例创建表单集
- sql-server - 增加 SQL VDI(虚拟设备接口)SQL 备份到 Azure Blob 返回的缓冲区大小
- azure - Azure 函数 - 仅部署 64 位应用程序
- google-chrome - 如何在电子中启用“按ESC退出全屏模式”
- python - 集合的所有元素都适合同一时间窗口吗?
- sql - 使用 T-SQL 转换 AD 时间戳
- r - ggplot2,x 轴不能准确显示刻度