TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Reylin Mathew
NA
347
75.3k
resize crop and image
Apr 5 2018 9:51 AM
I have a system here to upload an image and crop it using asp.net. When I save the cropped image, It won't resize and stay as the original. How do I resize the picture?
This is my code.
Default.aspx
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"resizepic.aspx.cs"
Inherits=
"WebApplication.resizepic"
%>
<!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>
<link href=
"css/jquery.Jcrop.css"
rel=
"stylesheet"
type=
"text/css"
/>
<script type=
"text/javascript"
src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"
></script>
<script src=
"script/jquery.Jcrop.js"
type=
"text/javascript"
></script>
<script type=
"text/javascript"
>
$(window).load(
function
() {
var
jcrop_api;
var
i, ac;
initJcrop();
function
initJcrop() {
jcrop_api = $.Jcrop(
'#imgCrop'
, {
onSelect: storeCoords,
onChange: storeCoords
//, onSelect: storeCoords(c)
});
jcrop_api.setOptions({ aspectRatio: 1 / 1 });
jcrop_api.setOptions({
minSize: [250, 250],
maxSize: [250, 350]
//,onSelect: storeCoords(c)
//onChange: SetCoordinates,
// onSelect: SetCoordinates
});
jcrop_api.setSelect([140, 180, 160, 180]);
};
function
storeCoords(c) {
jQuery(
'#X'
).val(c.x);
jQuery(
'#Y'
).val(c.y);
jQuery(
'#W'
).val(c.w);
jQuery(
'#H'
).val(c.h);
};
});
</script>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<asp:Panel ID=
"pnlUpload"
runat=
"server"
>
<asp:FileUpload ID=
"Upload"
runat=
"server"
/>
<asp:Button ID=
"btnUpload"
runat=
"server"
OnClick=
"btnUpload_Click"
Text=
"Upload"
/>
<asp:Label ID=
"lblError"
runat=
"server"
Visible=
"false"
/>
</asp:Panel>
<asp:Panel ID=
"pnlCrop"
runat=
"server"
Visible=
"false"
style=
"width:250px;height:auto"
>
<asp:Image ID=
"imgCrop"
runat=
"server"
/>
<asp:HiddenField ID=
"X"
runat=
"server"
/>
<asp:HiddenField ID=
"Y"
runat=
"server"
/>
<asp:HiddenField ID=
"W"
runat=
"server"
/>
<asp:HiddenField ID=
"H"
runat=
"server"
/>
</asp:Panel>
<asp:Panel ID=
"pnlCropped"
runat=
"server"
Visible=
"false"
>
<asp:Image ID=
"imgCropped"
runat=
"server"
/>
</asp:Panel>
<asp:Button ID=
"btnCrop"
runat=
"server"
Text=
"Crop"
OnClick=
"btnCrop_Click"
/>
</div>
</form>
</body>
</html>
Default.aspx.cs
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
btnUpload_Click(
object
sender, EventArgs e)
{
Boolean FileOK =
false
;
Boolean FileSaved =
false
;
if
(Upload.HasFile)
{
Session[
"UploadImage"
] = Upload.FileName;
String ExtensionofImage = Path.GetExtension(Session[
"UploadImage"
].ToString()).ToLower();
String[] allowed = {
".png"
,
".jpeg"
,
".jpg"
,
".gif"
};
for
(
int
i = 0; i < allowed.Length; i++)
{
if
(ExtensionofImage == allowed[i])
{
FileOK =
true
;
}
}
}
if
(FileOK)
{
try
{
Upload.PostedFile.SaveAs(path + Session[
"UploadImage"
]);
FileSaved =
true
;
}
catch
(Exception ex)
{
lblError.Text =
"Sorry Can't Upload"
+ ex.Message.ToString();
lblError.Visible =
true
;
FileSaved =
false
;
}
}
else
{
lblError.Text =
"Select some Other Image"
;
lblError.Visible =
true
;
}
if
(FileSaved)
{
pnlUpload.Visible =
false
;
pnlCrop.Visible =
true
;
imgCrop.ImageUrl =
"images/"
+ Session[
"UploadImage"
].ToString();
}
}
protected
void
btnCrop_Click(
object
sender, EventArgs e)
{
string
Img = Session[
"UploadImage"
].ToString();
int
w = Convert.ToInt32(W.Value);
int
h = Convert.ToInt32(H.Value);
int
x = Convert.ToInt32(X.Value);
int
y = Convert.ToInt32(Y.Value);
byte
[] CropImage = Cut(path + Img, w, h, x, y);
using
(MemoryStream mem =
new
MemoryStream(CropImage, 0, CropImage.Length))
{
mem.Write(CropImage, 0, CropImage.Length);
using
(SD.Image CroppedImage = SD.Image.FromStream(mem,
true
))
{
string
SaveTo = path +
"crop"
+ Img;
CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
pnlCrop.Visible =
false
;
pnlCropped.Visible =
true
;
imgCropped.ImageUrl =
"images/crop"
+ Img;
}
}
}
static
byte
[] Cut(
string
Img,
int
Breadth,
int
Length,
int
X,
int
Y)
{
try
{
using
(SD.Image OriginalImage = SD.Image.FromFile(Img))
{
using
(SD.Bitmap bmp =
new
SD.Bitmap(Breadth, Length))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using
(SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage,
new
SD.Rectangle(0, 0, Breadth, Length), X, Y, Breadth, Length, SD.GraphicsUnit.Pixel);
MemoryStream ms =
new
MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return
ms.GetBuffer();
}
}
}
}
catch
(Exception Ex)
{
throw
(Ex);
}
}
plz help me..
Reply
Answers (
1
)
How to get Inspect Element in ASP.Net C#
Loading dynamic XML files in to a tree view control