求asp.net上传图片并同时生成缩略图的代码啊 5
求asp.net上传图片并同时生成缩略图的代码啊希望各位发的不仅仅是CS代码,还有HTML的代码.我学的是C#的,最好是C#的,如果是VB的,我也学学.谢谢拉!麻烦你帮写...
求asp.net上传图片并同时生成缩略图的代码啊
希望各位发的不仅仅是CS代码,还有HTML的代码.
我学 的是C#的,最好是C#的,如果是VB的,我也学学.
谢谢拉!
麻烦你帮写或是找段代码给我,行吗?
不胜感激! 展开
希望各位发的不仅仅是CS代码,还有HTML的代码.
我学 的是C#的,最好是C#的,如果是VB的,我也学学.
谢谢拉!
麻烦你帮写或是找段代码给我,行吗?
不胜感激! 展开
3个回答
展开全部
上传图片到数据库
using System;
using System.Data;
using System.Drawing;
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;
}
}
'生成缩略图
image=System.Drawing.Image.FromFile(Server.MapPath("classpic/"+"rs1.jpg"))
width=image.Width
height=image.height
if width>height then
newwidth=110
newheight=image.height/image.Width*newwidth
else
newheight=110
newwidth=image.Width/image.height*newheight
end if
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())
aNewImage.Save(Server.MapPath("smallpic/"+"rs1.gif"))
image.Dispose()
using System;
using System.Data;
using System.Drawing;
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;
}
}
'生成缩略图
image=System.Drawing.Image.FromFile(Server.MapPath("classpic/"+"rs1.jpg"))
width=image.Width
height=image.height
if width>height then
newwidth=110
newheight=image.height/image.Width*newwidth
else
newheight=110
newwidth=image.Width/image.height*newheight
end if
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())
aNewImage.Save(Server.MapPath("smallpic/"+"rs1.gif"))
image.Dispose()
展开全部
image有这个函数哦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以下代码放在上传按钮的click中
HttpPostedFile
file
=
fileupbmp.PostedFile;
if
(file.ContentLength
!=
0)
{
//
string[]
filesplit
=
file.FileName.Split(new
Char[]
{
'\\'
});
//
string
filename
=
filesplit[filesplit.Length
-
1];
file.SaveAs(Server.MapPath("img/hourse/"
+
txtname.Text)
+
".jpg");
imgBmp.ImageUrl
=
"~/admin/img/hourse/"
+
txtname.Text
+
".jpg";
}
html中加入
<input
id="fileupbmp"
type="file"
runat="server"
style="width:
325px"
/>
<asp:Image
ID="imgBmp"
runat="server"
Height="121px"
Width="174px"
/>
HttpPostedFile
file
=
fileupbmp.PostedFile;
if
(file.ContentLength
!=
0)
{
//
string[]
filesplit
=
file.FileName.Split(new
Char[]
{
'\\'
});
//
string
filename
=
filesplit[filesplit.Length
-
1];
file.SaveAs(Server.MapPath("img/hourse/"
+
txtname.Text)
+
".jpg");
imgBmp.ImageUrl
=
"~/admin/img/hourse/"
+
txtname.Text
+
".jpg";
}
html中加入
<input
id="fileupbmp"
type="file"
runat="server"
style="width:
325px"
/>
<asp:Image
ID="imgBmp"
runat="server"
Height="121px"
Width="174px"
/>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询