上传图片到数据库
如题.
下面是一些方法
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2_click(object sender, EventArgs e)
{
string caption = textbox2.Text;
bool ispublic = false;
if (checkbox1.Checked)
ispublic = true;
Addalbum(caption, ispublic);
label1.Text = "album is ok! ";
}
public static void Addalbum(string caption, bool ispublic)
{
SqlConnection connection = new SqlCannection(ConfigurationManager.ConnectionStrings[ "personal "].ConnectionString);
string sql;
sql = "insert into [albums] ([caption],[ispublic]) values (@caption,@ispublic) ";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add(new SqlParameter( "@caption ", caption));
command.Parameters.Add(new SqlParameter( "@ispublic ", ispublic));
connection.Openn();
command.ExecuteNonQuery();
}
protected void button1_click(object sender, EventArgs e)
{
Stream imgdatastream = fileupload1.PostedFile.InputStream;
int imgdatalen = fileupload1.PostedFile.ContentLength;
string caption = texbox1.Text;
byte[] bytesoriginal = new byte[imgdatalen];
int n = imgdatastream.Read(bytesoriginal, 0, imgdatalen);
addphoto(1, caption, bytesoriginal);
label1.Text = "image is uploaded ";
}
public static void addphoto(int albumid, string caption, byte[] bytesoriginal)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[ "personal "].ConnectionString);
string sql;
sql = "insert into [photos] ([albumid],[bytesoriginal],[caption],[bytesfull],[bytesposter],[bytesthumb]) " + "values (@albumid,@bytesoriginal,@caption,@bytesfull,@bytesposter,@bytesthumb) ";
SqlCommand command = new SqlCommand(sql.connection);
command.Parameters.Add(new SqlParameter( "@albumid ", albumid));
command.Parameters.Add(new SqlParameter( "@caption ", caption));
command.Parameters.Add(new SqlParameter( "@bytesoriginal ", bytesoriginal));
command.Parameters.Add(new SqlParameter( "@bytesfull ", ReSizeImageFile(bytesoriginal, 600)));
command.Parameters.Add(new SqlParameter( "@bytesposter ", ReSizeImageFile(bytesoriginal, 198)));
command.Parameters.Add(new SqlParameter( "@bytesthumb ", ReSizeImageFile(bytesoriginal, 100)));
connection.Open();
command.ExecuteNonQuery();
}
private static byte[] ReSizeImageFile(byte[] imagefile, int targetSize)
{
System.Drawing.Image oldimage = System.Drawing.Image.FromStream(new MemoryStream(imagefile));
Size newSize = CalculateDimensions(oldimage.Size, targetSize);
bitmap newimage = new bitmap(newSize, width, newSize, height, PixelFormat.Format24bpprgb);
graphics canvas = graphics.Fromimage(newimage);
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldimage, new RectangleHotSpot(new point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newimage.Save(m, ImageFormat.Jpeg);
return m.GetBuffer();
}
private static Size CalculateDimensions(Size oldSize,int targetSize)
{
Size newSize=new Size();
if (oldSize.Height> oldSize.Width)
{
newSize.Width=(int)(oldSize.Width*((float)targetSize/(float)oldSize.Height));
newSize.Height=targetSize;
}
else
{
newSize.Width=targetSize;
newSize.Height=(int)(oldSize.Height*((float)targetSize/(float)oldSize.Width));
}
return newSize;
}
}
帮忙看看哪出错了谢谢
[解决办法]
要问你哪里报什么错误?
[解决办法]
在 程序 打上 断点
看是在 那里出的错.
[解决办法]
没有错误的代码
http://dotnet.aspx.cc/article/9154bc99-df64-4e2d-b096-26c99ce464be/read.aspx
http://dotnet.aspx.cc/article/ey1xldyv-pidf-43lo-1wfl-fmy5ale1f635/read.aspx
[解决办法]
using System.Drawing;