asp.net从数据库中读取图片

ExpandedBlockStart.gif
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 经过处理获取所略图
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="ms"></param>
InBlock.gif    
/// <param name="height"></param>
ExpandedBlockEnd.gif    
/// <returns></returns>

None.gif      private   byte [] LessonPicAutoWidth(MemoryStream ms,  int  height)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
byte[] ChagedByte;
InBlock.gif        System.Drawing.Image OriginalImage 
= System.Drawing.Image.FromStream(ms, true);
InBlock.gif        System.Drawing.Bitmap OriginalPic, NewPic;
InBlock.gif        OriginalPic 
= new System.Drawing.Bitmap(OriginalImage);
InBlock.gif
InBlock.gif        
int Width = height * OriginalPic.Width / OriginalPic.Height;
InBlock.gif
InBlock.gif        
if (Width > 280)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Width 
= 280;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        NewPic 
= new Bitmap(OriginalPic, Width, height);
InBlock.gif        MemoryStream Newms 
= new MemoryStream();
InBlock.gif        NewPic.Save(Newms, System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif        ChagedByte 
= Newms.GetBuffer();
InBlock.gif        OriginalPic.Dispose();
InBlock.gif        NewPic.Dispose();
InBlock.gif        Newms.Close();
InBlock.gif
InBlock.gif        
return ChagedByte;
ExpandedBlockEnd.gif    }
     /**/ /// <summary>
InBlock.gif    
/// 显示图片
InBlock.gif    
/// </summary>
ExpandedBlockEnd.gif    
/// <param name="EmployeeID"></param>

None.gif      public   void  GetPhoto( string  EmployeeID,  int  height)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Bitmap bmp;
InBlock.gif        
byte[] PhotoByte;
InBlock.gif        
string cnstr;
InBlock.gif        
string sql;
InBlock.gif        cnstr 
= "Data Source=localhost;Database=Northwind;uid=sa;pwd=123";
InBlock.gif        sql 
= "Select Photo From Employees Where EmplyeeID=" + EmployeeID;
InBlock.gif
InBlock.gif        
using (SqlConnection cn = new SqlConnection(cnstr))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlCommand cmd = cn.CreateCommand())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cn.Open();
InBlock.gif                cmd.CommandText 
= sql;
InBlock.gif                
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    reader.Read();
InBlock.gif                    PhotoByte 
= (byte[])reader.GetValue(0);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (PhotoByte == null || PhotoByte.Length == 0return;
InBlock.gif
            MemoryStream tempStream = new MemoryStream(PhotoByte);
InBlock.gif        PhotoByte 
= LessonPicAutoWidth(tempStream, height);
InBlock.gif        bmp 
= new Bitmap(tempStream);
InBlock.gif
InBlock.gif        Response.ContentType 
= "image/gif";
InBlock.gif        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
InBlock.gif        Response.End();
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/yuzhixue/archive/2006/10/18/532373.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要从数据库读取视频并在网页上显示,一种常见的方法是将视频文件的路径存储在数据库,然后从该路径读取视频文件并将其嵌入到网页。以下是一些基本步骤: 1. 在数据库创建一个包含视频文件路径的表。 2. 在ASP.NET网页上添加一个适当的控件,例如HTML5的video标签或ASP.NET的Media Player控件。 3. 在代码,使用ADO.NET连接到数据库读取视频文件的路径。 4. 将路径插入到网页控件,以便视频文件可以在网页上播放。 以下是一些示例代码: 1. 创建一个包含视频文件路径的表: CREATE TABLE Videos (ID INT PRIMARY KEY, FilePath VARCHAR(100)) 2. 在ASP.NET网页上添加一个适当的控件: <asp:MediaPlayer ID="MediaPlayer1" runat="server" /> 3. 在代码连接到数据库读取视频文件的路径: string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"; string queryString = "SELECT FilePath FROM Videos WHERE ID = @ID"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Parameters.AddWithValue("@ID", 1); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { string filePath = reader.GetString(0); MediaPlayer1.MediaUrl = filePath; } reader.Close(); } 4. 将路径插入到网页控件: MediaPlayer1.MediaUrl = filePath; 请注意,上述代码仅为示例,您需要根据您的数据库架构和ASP.NET网页的需求进行修改。此外,请确保正确处理视频文件的格式和大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值