Hi
So here is what I do:
- User click on their existing picture or blank pic placeholder
- I launch image editor in radwindow
- Now they can edit their pic, or upload a new and edit it.
- I on purpose left the save button from the toolbar, and added my own button that call RadImageEditor1_ImageSaving
- I only allow jpegs an also optimize and resize the image (I have 2000+ memners on the site and can;t afford each of them having a 1 - 2mb profile pic (you know regular non IT people, they just upload from their camera 12Mpixel photos)
(Still have to add the closing of the window, and default / existing image in the markup... and some other obvious parts not shown)
using
System;
using
System.Drawing;
using
System.Drawing.Drawing2D;
using
System.Drawing.Imaging;
using
System.IO;
using
System.Linq;
using
System.Web.Security;
using
Telerik.Web.UI;
using
Telerik.Web.UI.ImageEditor;
namespace
YourAppHere.Profile
{
public
partial
class
PersonalInformation : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
RadImageEditor1_ImageSaving(
object
sender, ImageEditorSavingEventArgs args)
{
MembershipUser mu = Membership.GetUser();
string
picpath = String.Format(
"~/Profile/Pictures/{0}.jpg"
, mu.UserName);
Image optimizedimage = RezizeImage(args.Image.Image, 320, 320);
optimizedimage.Save(MapPath(picpath), System.Drawing.Imaging.ImageFormat.Jpeg);
args.Cancel =
true
;
}
protected
void
AsyncUpload1_FileUploaded(
object
sender, FileUploadedEventArgs e)
{
//Clear changes and remove uploaded image from Cache
RadImageEditor1.ResetChanges();
Context.Cache.Remove(Session.SessionID +
"UploadedFile"
);
using
(Stream stream = e.File.InputStream)
{
byte
[] imgData =
new
byte
[stream.Length];
stream.Read(imgData, 0, imgData.Length);
MemoryStream ms =
new
MemoryStream();
ms.Write(imgData, 0, imgData.Length);
Context.Cache.Insert(Session.SessionID +
"UploadedFile"
, ms,
null
, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
}
}
protected
void
RadImageEditor1_ImageLoading(
object
sender, ImageEditorLoadingEventArgs args)
{
//Handle Uploaded images
if
(!Object.Equals(Context.Cache.Get(Session.SessionID +
"UploadedFile"
),
null
))
{
using
(EditableImage image =
new
EditableImage((MemoryStream)Context.Cache.Remove(Session.SessionID +
"UploadedFile"
)))
{
args.Image = image.Clone();
args.Cancel =
true
;
}
}
}
private
System.Drawing.Image RezizeImage(System.Drawing.Image img,
int
maxWidth,
int
maxHeight)
{
if
(img.Height < maxHeight && img.Width < maxWidth)
return
img;
Double xRatio = (
double
)img.Width / maxWidth;
Double yRatio = (
double
)img.Height / maxHeight;
Double ratio = Math.Max(xRatio, yRatio);
int
nnx = (
int
)Math.Floor(img.Width / ratio);
int
nny = (
int
)Math.Floor(img.Height / ratio);
Bitmap cpy =
new
Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
Graphics gr = Graphics.FromImage(cpy);
gr.Clear(Color.Transparent);
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage(img,
new
Rectangle(0, 0, nnx, nny),
new
Rectangle(0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
gr.Dispose();
img.Dispose();
return
cpy;
}
}
}
<
script
type
=
"text/javascript"
>
(function (global, undefined) {
var demo = {};
function OnClientFilesUploaded(sender, args) {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest();
}
function serverID(name, id) {
demo[name] = id;
}
global.serverID = serverID;
global.OnClientFilesUploaded = OnClientFilesUploaded;
})(window);
function editProfilerPic() {
var oWnd = $find("<%=RadWindow_ProfilePic.ClientID %>");
oWnd.Show();
}
function saveChangesInImageEditor() {
var imEditor = $find("<%= RadImageEditor1.ClientID %>");
imEditor.saveImageOnServer("xxxxxx.jpg", true);
}
</
script
>
<
style
type
=
"text/css"
>
.rieDialogs { z-index: 10000; }
.rwWindowContent>div {overflow:hidden !important;}
.RadImageEditor_Bootstrap {outline:none !important}
.RadToolBar_Bootstrap_Horizontal {margin-right:10px !important;}
table.tbl {width:100%; border-collapse:collapse; border:none; }
table.tbl td {padding:0px; }
table.tbl td.tblr {padding-right:5px; text-align:right;}
input[type=button] {outline:none !important;}
</
style
>
<
telerik:RadWindow
runat
=
"server"
ID
=
"RadWindow_ProfilePic"
Title
=
"Profile Image Editor"
ShowContentDuringLoad
=
"false"
Modal
=
"true"
Overlay
=
"True"
Width
=
"622px"
Height
=
"622px"
VisibleStatusbar
=
"False"
Behaviors
=
"Close"
Animation
=
"Fade"
>
<
ContentTemplate
>
<
div
style
=
"width:605px; height: 570px; overflow:hidden; padding-top:10px"
>
<
table
class
=
"tbl"
>
<
tr
>
<
td
>
<
telerik:RadAsyncUpload
ID
=
"AsyncUpload1"
runat
=
"server"
OnClientFilesUploaded
=
"OnClientFilesUploaded"
OnFileUploaded
=
"AsyncUpload1_FileUploaded"
MaxFileSize
=
"1048576"
AllowedFileExtensions
=
"jpg"
AutoAddFileInputs
=
"false"
Localization-Select
=
"Upload"
/>
</
td
>
<
td
class
=
"tblr"
>
<
input
type
=
"button"
class
=
"btn btn-default"
onclick
=
"saveChangesInImageEditor();"
value
=
"Save and Close"
/>
</
td
>
</
tr
>
</
table
>
<
asp:Label
ID
=
"Label1"
Text
=
"Size limit: 1 MB - File Types: Only jpg files allowed"
runat
=
"server"
Style
=
"font-size: 11px;"
EnableViewState
=
"false"
></
asp:Label
>
<
telerik:RadImageEditor
ID
=
"RadImageEditor1"
runat
=
"server"
Width
=
"578"
Height
=
"485"
ImageUrl
=
"~/Profile/Pictures/test.jpg"
OnImageLoading
=
"RadImageEditor1_ImageLoading"
EnableResize
=
"False"
ShowAjaxLoadingPanel
=
"True"
UndoLimit
=
"5"
ToolBarPosition
=
"Top"
onimagesaving
=
"RadImageEditor1_ImageSaving"
>
<
Tools
>
<
telerik:ImageEditorToolGroup
>
<
telerik:ImageEditorToolStrip
Text
=
"Undo"
CommandName
=
"Undo"
/>
<
telerik:ImageEditorToolStrip
Text
=
"Redo"
CommandName
=
"Redo"
/>
<
telerik:ImageEditorTool
Text
=
"Reset"
CommandName
=
"Reset"
/>
</
telerik:ImageEditorToolGroup
>
<
telerik:ImageEditorToolGroup
>
<
telerik:ImageEditorTool
Text
=
"Crop"
CommandName
=
"Crop"
IsToggleButton
=
"true"
/>
<
telerik:ImageEditorTool
Text
=
"Resize"
CommandName
=
"Resize"
IsToggleButton
=
"true"
/>
<
telerik:ImageEditorTool
Text
=
"Zoom"
CommandName
=
"Zoom"
/>
<
telerik:ImageEditorTool
Text
=
"ZoomIn"
CommandName
=
"ZoomIn"
/>
<
telerik:ImageEditorTool
Text
=
"ZoomOut"
CommandName
=
"ZoomOut"
/>
</
telerik:ImageEditorToolGroup
>
<
telerik:ImageEditorToolGroup
>
<
telerik:ImageEditorTool
Text
=
"Brightness Contrast"
CommandName
=
"BrightnessContrast"
IsToggleButton
=
"true"
/>
<
telerik:ImageEditorTool
Text
=
"Greyscale"
CommandName
=
"Greyscale"
/>
<
telerik:ImageEditorTool
Text
=
"Hue Saturation"
CommandName
=
"HueSaturation"
IsToggleButton
=
"true"
/>
</
telerik:ImageEditorToolGroup
>
</
Tools
>
</
telerik:RadImageEditor
>
</
div
>
</
ContentTemplate
>
</
telerik:RadWindow
>