C#(WinForm)上传图片到服务器

C#(WinForm)上传图片到服务器,第1张

//本地上传代码Files item = fis as Files

WebClient wc = new WebClient()

string url = string.Format("{0}?Overwrite=true&Path={1}", "服务器上传地址", item.Path)

 wc.UploadFile(url, "POST", item.Path)

 //服务器接收

 string ServerSrc = context.Server.MapPath("~/DownLogin/")

        foreach (string filekey in context.Request.Files)

        {

            HttpPostedFile file = context.Request.Files[filekey]

            string FilePath = Path.Combine(ServerSrc, file.FileName)

            if (File.Exists(FilePath))

            {

                if (Convert.ToBoolean(context.Request["overwrite"]))

                {

                    File.Delete(FilePath)

                }

                else

                    continue

            }

            file.SaveAs(FilePath)

        }

写:

FileStream fs = new FileStream("图片路径", FileMode.Open)

Byte[] imagebytes = new byte[fs.Length] //二进制转换

BinaryReader br = new BinaryReader(fs)

imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length))//读取二进制流

读:

//dr["content_"]为数据库保存的二进制图片数据

MemoryStream ms = new MemoryStream((byte[])dr["content_"])//把照片读到MemoryStream里

Image imageBlob = Image.FromStream(ms, true)//用流创建Image

pictureBox1.Image = imageBlob//输出图片

下面有一段代码,在我一个项目里抽出来的,没有完全T出来,你应该看得懂了:

Stream ms

byte[] picbyte

OpenFileDialog ofdSelectPic = new OpenFileDialog()

ofdSelectPic.DefaultExt = "*.bmp*.jpg*.jpeg*.gif*.png"

ofdSelectPic.Filter = "*.jpg,*.gif,*.bmp,*.png,*.jpeg|*.jpg*.gif*.bmp*.png*.jpeg"

if (ofdSelectPic.ShowDialog() == DialogResult.OK)

{

if ((ms = ofdSelectPic.OpenFile()) != null)

{

string filepath = ofdSelectPic.FileName

string extension = Path.GetExtension(filepath)

if (extension == ".jpg" || extension == ".gif" || extension == ".bmp" || extension == ".jpeg" || extension == ".png")

{

if (SelectImageIndex == 1)

{

picSignature.Image = Image.FromFile(filepath)

}

else if (SelectImageIndex == 2)

{

picphotoid1.Image = Image.FromFile(filepath)

}

else if (SelectImageIndex == 3)

{

picphoto2.Image = Image.FromFile(filepath)

}

picbyte = new byte[ms.Length]

ms.Position = 0

ms.Read(picbyte, 0, Convert.ToInt32(ms.Length))

ms.Close()

return picbyte

}

else

{

MessageBox.Show("you can only select the image type file!")

return null

}

}

else

{

return null

}

}

那个SQL数据库里字段设置成image或者binary都行。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10875023.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存