|
SqlCommand comm=conn.CreateCommand();
comm.CommandText="select
* from Images where id=@id";
comm.CommandType=CommandType.Text;
comm.Parameters.Add("@id", SqlDbType.BigInt).Value=int.Parse(Request["id"]);
SqlDataReader reader=comm.ExecuteReader();
while(reader.Read())
{
Response.ContentType=reader["contentType"].ToString();
Response.BinaryWrite((byte[])reader["Image"]);
}
Response.End();
conn.Close();
}
catch
{
Response.End();
}
这段代码可置于Page_Load事件中,数据图片要注意的两点是:
一、
设置正确的ContentType(http中的content-type),图片的content-type格式一般为image/*,如jpeg为image/jpeg,bmp为image/bmp等等。
二、
仅仅输出一张图片二进制流,asp.net 中Page_Load事件先于页面输出被触发,因此图片的输出可以在此事件中进行,直接操作Reponse对象,避免输出与图片无关的而外信息(额外的第二张图片或者文字)。图片的二进制流输出后及时使用Response.End()方法结束http响应,避免页面中的额外信息被asp.net的引擎默认输出到客户端。
希望此文能够起到抛砖引玉的作用!^_^
附录一:
MainForm.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
namespace InsertImageToDatabase
{
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDlg;
private System.Windows.Forms.TextBox filePath;
private System.Windows.Forms.Button browseButton;
private System.Windows.Forms.Button insertButton;
/// <summary>
/// Required designer
variable.
/// </summary>
private System.ComponentModel.Container
components = null;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
call
//
}
/// <summary>
/// Clean up any resources
being used.
/// </summary>
protected override void Dispose( bool disposing )
{
上一页 [1] [2] [3] [4] [5] [6] 下一页 [C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
|