还有在pictureBox 中显示的问题,请赐教详细的代码,谢谢,顺祝各位大侠元旦快乐 !
解决方案 »
- C#后台编码格式
- 如何计算文本的大小
- web下载文件的锁定是怎样做的?
- 鼠标移动显示坐标x,y轴,鼠标停止移动显示提示信息,图象放大缩小等问题请进!!!
- 使用Microsoft Visual SourceSafe问题
- 大家说用对象好还是字符串的拼接好?
- 请教!新人分不多,一下给了100,所以一下多提几个问题,希望能得到解答,谢谢~
- 请帅哥推荐WinForm的好书,最好是有电子版的可以下载。谢谢啊
- 如何求一个判断表达式的布尔值
- c#处理5000条JSON的问题,大佬来指导一下
- 想问一下大家写C#应用程序时显示数据,写数据是用数据源,还是自己写的?
- DataGridView中如何设置当前单元格的背景色
一,上传并存入SqlServer
数据库结构
create table test
{
id identity(1,1),
FImage image
}
相关的存储过程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength); //连接数据库
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa"; SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray; //如果你希望不使用存储过程来添加图片把上面四句代码改为:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray; conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}二,从SqlServer中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>ShowPhoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql="select * from test where id=2";//这里假设获取id为2的图片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}
byte[] fb = new byte[fs.Length];
fs.Read(fb, 0, (int)fs.Length);
string strSql = "INSERT INTO tb1_test(ID,Name,pic) VALUES(4,'zhengce',@file)"; //sql语句,其中保存图片的pic字段为Image类型
string sConn = "Data Source=127.0.0.1;Initial Catalog=test;User ID=sa;Password=sa";//连接字符串
SqlConnection myConn;
SqlCommand myCom;
myConn = new SqlConnection(sConn);
try
{
myConn.Open();
myCom = new SqlCommand(strSql, myConn);
myCom.Parameters.Add("@file", SqlDbType.Image);
myCom.Parameters["@file"].Value = fb;
myCom.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
myConn.Close();
}
{
uid = Session["UserId"].ToString();
FilmVobInfo fvi = (FilmVobInfo)Session[Session.SessionID + FilmVobInfo.UserIdString];
UploadDB upload = new UploadDB(); bool fileOK = false;
if (FileUpload1.PostedFile.ContentLength > 1)
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string fileName_s = "s_" + file.Name; // 缩略图文件名称
string webFilePath = Server.MapPath("~/upload/" + fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath("~/upload/" + fileName_s);
fviImgPath = "upload/" + fileName_s;
//string size = FileUpload1.PostedFile.ContentLength.ToString();
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 MakeThumbnail(webFilePath, webFilePath_s, 120, 90, "Cut"); // 生成缩略图方法
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] alloweExtensions ={ ".gif", ".jpg", ".bmp", "img", "imge", "png" };
for (int i = 0; i < alloweExtensions.Length; i++)
{
if (fileExtension == alloweExtensions[i])
{
fileOK = true;
}
} }
} else
{ fvi.ImgPath = "Images/" + "暂无图片.jpg";
} if (Session[Session.SessionID + FilmVobInfo.UserIdString] == null)
{
return;
} if (upload.AddBC( fvi.ImgPath) > -1)
{ Panel1.Visible = true; //Panel上放一些上传成功的文字信息等等。 }
} ///生成缩略图
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
这是俺做的,也是通过访照别的例子,