用户在网站上传照片时如何查看照片尺寸。 [英] how to check photo size when user upload a photo on the website.
本文介绍了用户在网站上传照片时如何查看照片尺寸。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i有网络项目,接受用户详细信息并将这些详细信息保存在sql server表中。
i想要的时候用户上传个人资料照片然后服务器检查照片大小并回复用户他已经上传了4 kb照片。
照片上传代码是....
页面设计代码
Hi all,
i have web project,that accept user details and save that details in sql server table.
i want that when user upload profile photo then server check the photo size and reply the user that he has upload only 4 kb photo.
photo upload code is ....
page design code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PhotoPanel.aspx.cs" Inherits="PhotoPanel" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font: normal 12px/15px verdana;
background-color: #f1f5fa;
}
.w_button
{
border: 1px solid #135C86;
background: -webkit-gradient( linear, left top, left bottom, color-stop(0.1, #7EC5E2), color-stop(0.9, #3692C6) );
background: -moz-linear-gradient( center top, #7EC5E2 10%, #3692C6 90% );
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#7EC5E2', endColorstr='#3692C6');
background-color: #7EC5E2;
-moz-box-shadow: inset 0px 1px 2px 0px #FFFFFF;
-webkit-box-shadow: inset 0px 1px 2px 0px #FFFFFF;
box-shadow: inset 0px 1px 2px 0px #FFFFFF;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
text-shadow: 1px 1px 2px #5a5b5b;
font-weight: bold;
margin: 5px 5px;
padding: 6px;
color: #FFFFFF;
letter-spacing: 0.5px;
font-family: 'Arial', sans-serif;
font-size: 16px;
width: 100%;
text-transform: capitalize;
text-align: center;
text-decoration: none;
cursor: pointer;
display: inline-block;
}
#pp_main
{
width: 375px;
color:#414141;
padding: 5px 10px;
background-color: #f1f5fa;
}
.pp_cp
{
background: none repeat scroll 0 0 #09458D;
color: #FFFFFF;
font-weight: bold;
margin-bottom: 10px;
padding: 3px;
text-align: center;
text-transform: uppercase;
}
.pp_img
{
width: auto;
height: auto;
float:left;
}
.pp_fileup_button
{
float:left;
margin-left: 7px;
}
.pp_name
{
margin: 10px 0 20px;
}
.name
{
text-transform: capitalize; font:bold 17px/17px verdana; color:#000;
}
.pp_fileup
{
margin: 5px 0;
}
.pp_button
{
margin: 10px 0;
}
.clr
{
clear:both;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="pp_main">
<div class="pp_cp">
<span>Change Your Profile Pic</span>
</div>
<div>
<div class="pp_img">
<asp:Image ID="Image1" CssClass="pc" runat="server" Width="120" Height="120" ImageUrl="~/images/Avatar.jpg"
alt="avatar" />
</div>
<div class="pp_fileup_button">
<div class="pp_name">
<asp:Label ID="Label6" runat="server" class="name"></asp:Label>
</div>
<div class="pp_fileup">
<asp:FileUpload runat="server" ID="flImage1"></asp:FileUpload>
</div>
<div class="pp_button">
<%--<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/save.png"
onclick="ImageButton1_Click" />--%>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" class="w_button" />
</div>
</div>
<div class="clr"></div>
<asp:Label ID="Label1" runat="server" ForeColor="White" Font-Bold="True" Font-Italic="True"
Font-Size="Large" Visible="false"></asp:Label>
</div>
</div>
</form>
</body>
</html>
代码文件
Code file
public partial class PhotoPanel : System.Web.UI.Page
{
String constr = Convert.ToString(ConfigurationManager.ConnectionStrings["WizomCon"].ConnectionString);
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["usr"].ToString() != null)
{
Image1.ImageUrl = "~/ImgHandler.ashx?id=" + Session["usr"].ToString();
Label6.Text = Convert.ToString(Session["usr"]);
}
}
catch
{
Response.Redirect("~/Wizom_Login.aspx");
}
}
private byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
{
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;
}
//protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
//{
//}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (!flImage1.HasFile)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Must Choose An .jpeg,.png Image');", true);
}
else if (Session["usr"].ToString() != null)
{
FileUpload fu = flImage1;
String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
try
{
if (IsPostBack)
{
SqlConnection con = new SqlConnection(constr);
System.Drawing.Image imag = System.Drawing.Image.FromStream(flImage1.PostedFile.InputStream);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string un = Session["usr"].ToString();
SqlCommand cmd = new SqlCommand("SPUploadImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = un;
cmd.Parameters.Add("@image", SqlDbType.Image, 0).Value = ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("WizomProfile.aspx");
}
catch
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('There was an upload error, Make Sure To Upload a .jpeg,.png or .gif file');", true);
}
}
}
}
}
catch
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('There was an upload error.<br>Make Sure To Upload a .jpeg,.png or .gif file Try Again);", true);
}
}
}
推荐答案
Refer to these links. Modify it according to your needs.I hope it woul be helpful.
image height and width from fileupload and textbox field[^]
How to get the image width and height[^]
ASP.NET: get height and width of an image[^]
Validate Image Size, Dimension, and Type Uploads[^]
[Another Workaround]:
ASP.NET Image uploading with Resizing[^]
Regards.. :laugh:
Refer to these links. Modify it according to your needs.I hope it woul be helpful.
image height and width from fileupload and textbox field[^]
How to get the image width and height[^]
ASP.NET: get height and width of an image[^]
Validate Image Size, Dimension, and Type Uploads[^]
[Another Workaround]:
ASP.NET Image uploading with Resizing[^]
Regards.. :laugh:
这篇关于用户在网站上传照片时如何查看照片尺寸。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文