AjaxControlToolkitソースをダウンロードして、Server/AjaxControlToolkit/HtmlEditorExtender/HtmlEditorExtender.cs
ファイルをカスタマイズする必要があります。以下のプロパティをHtmlEditorExtenderクラスに追加します。
[Browsable(true)]
[DefaultValue(false)]
public bool ResizeUploadedImages { get; set; }
[Browsable(true)]
[DefaultValue(int.MaxValue)]
public int UploadedImageMaxWidth { get; set; }
[Browsable(true)]
[DefaultValue(int.MaxValue)]
public int UploadedImageMaxHeight { get; set; }
また、OnInitメソッドを次のように修正します。
/// <summary>
/// On Init add popup div and ajaxfileupload control to support Add image
/// </summary>
/// <param name="e">Event Arguments</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!DesignMode)
{
// Check if EnableSanitization is enabled and sanitizer provider is not configured.
if (EnableSanitization && sanitizerProvider == null)
{
throw new Exception("Sanitizer provider is not configured in the web.config file. If you are using the HtmlEditorExtender with a public website then please configure a Sanitizer provider. Otherwise, set the EnableSanitization property to false.");
}
HtmlGenericControl popupdiv = new HtmlGenericControl("div");
popupdiv.Attributes.Add("Id", this.ClientID + "_popupDiv");
popupdiv.Attributes.Add("style", "opacity: 0;");
popupdiv.Attributes.Add("class", "popupDiv");
ajaxFileUpload = new AjaxFileUpload();
ajaxFileUpload.ID = this.ID + "_ajaxFileUpload";
ajaxFileUpload.MaximumNumberOfFiles = 10;
ajaxFileUpload.AllowedFileTypes = "jpg,jpeg,gif,png";
ajaxFileUpload.Enabled = true;
ajaxFileUpload.OnClientUploadComplete = "ajaxClientUploadComplete";
if(ResizeUploadedImages)
{
ajaxFileUpload.UploadComplete += (sender, args) =>
{
var content = args.GetContents();
var resized = ResizeImage(content, UploadedImageMaxWidth, UploadedImageMaxHeight);
args.SetContents(resized);
};
}
if (ImageUploadComplete != null)
{
ajaxFileUpload.UploadComplete += ImageUploadComplete;
}
popupdiv.Controls.Add(ajaxFileUpload);
HtmlGenericControl btnCancel = new HtmlGenericControl("div");
btnCancel.Attributes.Add("Id", this.ClientID + "_btnCancel");
btnCancel.Attributes.Add("style", "float: right; position:relative; padding-left: 20px; top:10px; width: 55px; border-color:black;border-style: solid; border-width: 1px;cursor:pointer;");
btnCancel.Attributes.Add("float", "right");
btnCancel.Attributes.Add("unselectable", "on");
btnCancel.InnerText = "Cancel";
popupdiv.Controls.Add(btnCancel);
this.Controls.Add(popupdiv);
}
}
画像のサイズ変更に次のコードを使用しました。
private byte[] ResizeImage(byte[] imageBytes, int maxWidth, int maxHeight)
{
using (var memStream = new MemoryStream(imageBytes))
{
return ResizeImage(memStream, maxWidth, maxHeight);
}
}
/// <summary>
/// Resizes images to the specified size.
/// </summary>
/// <param name="imageStream">The file stream.</param>
/// <param name="maxWidth">Maximum Width</param>
/// <param name="maxHeight">Maximum Height</param>
private byte[] ResizeImage(System.IO.Stream imageStream, int maxWidth, int maxHeight)
{
byte[] result;
try
{
using (System.Drawing.Bitmap originalBMP = new System.Drawing.Bitmap(imageStream))
{
// Calculate the new image dimensions
int width = originalBMP.Width; //actual width
int height = originalBMP.Height; //actual height
int widthDiff = (width - maxWidth); //how far off maxWidth?
int heightDiff = (height - maxHeight); //how far off maxHeight?
//figure out which dimension is further outside the max size
bool doWidthResize = (maxWidth > 0 && width > maxWidth && widthDiff > -1 && (widthDiff > heightDiff || maxHeight.Equals(0)));
bool doHeightResize = (maxHeight > 0 && height > maxHeight && heightDiff > -1 && (heightDiff > widthDiff || maxWidth.Equals(0)));
//only resize if the image is bigger than the max or where image is square, and the diffs are the same
if (doWidthResize || doHeightResize || (width.Equals(height) && widthDiff.Equals(heightDiff)))
{
int iStart;
Decimal divider;
if (doWidthResize)
{
iStart = width;
divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
width = maxWidth;
height = (int)Math.Round((Decimal)(height / divider));
}
else
{
iStart = height;
divider = Math.Abs((Decimal)iStart / (Decimal)maxHeight);
height = maxHeight;
width = (int)Math.Round((Decimal)(width / divider));
}
}
using (System.Drawing.Bitmap newBMP = new System.Drawing.Bitmap(originalBMP, width, height))
{
using (System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(newBMP))
{
oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
oGraphics.DrawImage(originalBMP, 0, 0, width, height);
oGraphics.Save();
}
result = (new ImageConverter()).ConvertTo(newBMP, typeof(byte[])) as byte[];
}
}
}
catch (Exception)
{
result = null;
}
return result;
}
ところで、Server/AjaxControlToolkit/AjaxFileUpload/AjaxFileUploadEventArgs
アップロードされたファイルの内容を変更できるようにファイルを変更する必要もあります。次のようにSetContents
メソッドを追加する必要があります。
internal void SetContents(byte[] value)
{
_contents = value;
}