快速创建图像 [英] Create Image on the Fly

查看:110
本文介绍了快速创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

推荐


我正在asp.net中开发一个模块.用户使用文件上传控件将图片提交到服务器的位置.在服务器上,我必须创建发布文件的映像,而不必将其保存在服务器上.然后,我必须将其保存到不在服务器上的sql server数据库中.因此需要帮助

在某种程度上,我已经编码了一部分,但是主要部分要完成

Respected


I am developing a module in asp.net. Where a user submits a picture using fileupload control to the server. At server i Have to create image of the posted file without saving it on server. Then i have to save it to sql server database not on server. So help is needed

To Some Extent I have coded some of the portion but the major portion is to be done

Stream fi = FileUpload1.PostedFile.InputStream;
var ext = FileUpload1.PostedFile.ContentType;
var imageOrNot = ext.Contains("image") ? true : false;

if (imageOrNot)
{
      byte[] imagearr = new byte[fi.Length];
      fi.Read(imagearr, 0, (int)fi.Length);
      fi.Close();
      Response.Write(imagearr);
      MemoryStream ms = new System.IO.MemoryStream(imagearr);
      System.Drawing.Image imgTemp = System.Drawing.Image.FromStream(ms);
 
     FileStream fs = File.OpenRead(FileUpload1.PostedFile.FileName);
     //byte[] ba = new byte[fs.Length];
     //int length = fs.Read(ba, 0, ba.Length);


在Advance中致谢


Thanks in Advance

推荐答案

Letz假定数据库字段名称为EmployeeImg,具有SQL Server的图像属性.代码为:
Letz assume DB field name is EmployeeImg with image attribute of SQL Server. Code will be:
SqlConnection connection = null;
  try
  {
      FileUpload img = (FileUpload)imgUpload;
      Byte[] imgByte = null;
      if (img.HasFile && img.PostedFile != null)
      {
          HttpPostedFile File = imgUpload.PostedFile;
          imgByte = new Byte[File.ContentLength];
          File.InputStream.Read(imgByte, 0, File.ContentLength);
      }
      string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
      connection = new SqlConnection(conn);

      connection.Open();
     string sql = "INSERT INTO EmpDetails(EmployeeImg) VALUES(@EmployeeImg)";
      SqlCommand cmd = new SqlCommand(sql, connection);
      cmd.Parameters.AddWithValue("@EmployeeImg", imgByte);
      int id = Convert.ToInt32(cmd.ExecuteScalar());
  }


C#相册查看器 [ ^ ]


这篇关于快速创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆