如题.
下面是一些方法
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;
}
}帮忙看看哪出错了谢谢
下面是一些方法
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
无法显示 XML 页。
使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。
--------------------------------------------------------------------------------名称以无效字符开头。处理资源 'http://localhost/pws/upload.aspx' 时出错。第 1 行,位置: 2 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
-^