字节转换为asp.net中的字节数? [英] Bytes to bytes Conversion in asp.net?
本文介绍了字节转换为asp.net中的字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (fpPhoto.HasFile)
{
if (fpPhoto.PostedFile.ContentType == "image/jpg" || fpPhoto.PostedFile.ContentType == "image/jpeg" || fpPhoto.PostedFile.ContentType == "image/png")
{
byte[] imagebytes = new byte[fpPhoto.PostedFile.ContentLength];
int filelenght = fpPhoto.PostedFile.ContentLength;
imagebytes = fpPhoto.FileBytes;
fpPhoto.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
}
}
User objUser = new User();
objUser.UserName_Pk = txtUserName.Text;
objUser.Password = txtPassword.Text;
objUser.MobileNo = txtMobileNo.Text;
objUser.Email = txtEmail.Text;
objUser.SecurityAnswer = txtAnswer.Text;
objUser.Photo = Convert.ToByte(imagebytes);//Here is the problem
objUserBll.InsertUpdate(objUser);
推荐答案
如果您尝试将其转换为图像 - 和我想不出你想要把它转换成什么 - 然后试试这个:
If you are trying to convert it to an image - and I can't think of anything else you'd want to convert it to - then try this:
MemoryStream ms = new MemoryStream(imagebytes);
Image returnImage = Image.FromStream(ms);
但是......你需要移动你的定义:imagesbytes超出了你试图使用它的范围。
试试这个:
But...you need to move your definitions about anyway: imagesbytes is out of scope where you are trying to use it.
Try this:
Image userImage = null;
if (fpPhoto.HasFile)
{
if (fpPhoto.PostedFile.ContentType == "image/jpg" ||
fpPhoto.PostedFile.ContentType == "image/jpeg" ||
fpPhoto.PostedFile.ContentType == "image/png")
{
userImage = Image.FromStream(fpPhoto.PostedFile.InputStream);
}
}
User objUser = new User();
objUser.UserName_Pk = txtUserName.Text;
objUser.Password = txtPassword.Text;
objUser.MobileNo = txtMobileNo.Text;
objUser.Email = txtEmail.Text;
objUser.SecurityAnswer = txtAnswer.Text;
objUser.Photo = userImage;
objUserBll.InsertUpdate(objUser);
问题在于imagebytes
是一个字节数组。
因此你永远无法将它转换成单个字节,这就是Convert.ToByte()所做的。
这段代码究竟应该做什么? User.Photo是什么类型的数据类型?
The problem is thatimagebytes
is a byte array.
As such you will never be able to convert it into a single byte, which is what Convert.ToByte() does.
What exactly is this code supposed to do? What sort of data type is User.Photo?
这篇关于字节转换为asp.net中的字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文