//图片黏贴功能
IDataObject iData = Clipboard.GetDataObject();
imgzt =(Bitmap)iData.GetData(DataFormats.Bitmap); //存储数据库原始截图
if (imgzt != null)
{
Bitmap image = (Bitmap)iData.GetData(DataFormats.Bitmap); //缩小比例图片
image = new System.Drawing.Bitmap(image, 223, 297);//固定比例
pic_zhutu.Image = image;
}
else
{
MessageBox.Show("请用QQ截图后在点击图片框!");
}
上面的代码是复制QQ截图后,黏贴的图片,想请问下这个图片怎么把他上传到服务器上面呢!
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
这样bitmap存入了字节数组,至于怎么传输到服务器有很多办法。比如用socket或者webclient,你自己应该会的。
public string UploadImg(byte[] fileBytes, int id)
{
try
{
string filePath = MapPath(".") + "\EmpImage\" + id + ".jpg"; //图片要保存的路径及文件名
using (MemoryStream memoryStream = new MemoryStream(fileBytes))//1.定义并实例化一个内存流,以存放提交上来的字节数组。
{
using (FileStream fileUpload = new FileStream(filePath, FileMode.Create))//2.定义实际文件对象,保存上载的文件。
{
memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件
}
}
//GetSqlExcuteNonQuery是我写好的一个执行command的ExcuteNonQuery()方法
GetSqlExcuteNonQuery(string.Format("insert into EmpImg values('{0}','{1}')", id, filePath)); //将该图片保存的文件路径写入数据库
}
catch (Exception ex)
{
return ex.Message;
}
}