Afternoon, i am trying to upload a file using the asp.net FileUpload control. Before saving the image i need to rename the file with the users GUID, then resize the image to 128px.
However when it goes to save i am getting the error A generic error occurred in GDI+. and i can not work out how to get this sorted.
Could someone look over my code and provide some guidance please
protected void btnUpload_Click(object sender, EventArgs e)
{
Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
string directory = Server.MapPath("imgs/users");
string fileExt = Path.GetExtension(fuSample.PostedFile.FileName);
string fileName = userGuid + fileExt;
//Check File ext, make sure its an image!
if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif")
{
//Now we check to make sure its less than 600kb
if (fuSample.PostedFile.ContentLength < 614400)
{
//Delete an existing file.
if (File.Exists(directory + fileName))
File.Delete(directory + fileName);
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(fuSample.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 128;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(directory + fileName);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
//Success!
}
else
{
//error here, img too big
}
}
else
{
notifybar.Attributes.Add("style", "display:block;");
notifybar.Attributes.Add("class", "failed");
notifyText.Text = "Valid Image files only please! (.png, .jpg, .jpeg, or .gif)";
}
}
It could be something as simple as a file overwrite conflict. There are many dangerous bugs in this code, however, and it's not using RAM very efficiently.
I would suggest a different approach (using the ImageResizer NuGet package):
var j = new ImageJob(fuSample,"~/imgs/users/<guid>.<ext>",
new ResizeSettings("maxwidth=128;maxheight=128"));
j.Build();
string imageUrl = PathUtils.GuessVirtualPath(j.FinalPath)
When I'm trying to delete the images uploaded by me via website named "SampleApplication" I can see the following error shown in Stack Trace
The process cannot access the file 'D:\Hosting\123456\html\App_Images\myfolder1\eKuK2511.png' because it is being used by another process.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: The process cannot access the file 'D:\Hosting\123456\html\App_Images\myfolder1\eKuK2511.png' because it is being used by another process.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[IOException: The process cannot access the file 'D:\Hosting\123456\html\App_Images\myfolder1\eKuK2511.png' because it is being used by another process.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +9723350
System.IO.File.Delete(String path) +9545728
SampleApplication.BasePage.DeleteApp_ImagesById(DataTable dt) +503
SampleApplication.PostLease.MyAccount.DeleteAd_Property(Object sender, EventArgs e) +193
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +118
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +113
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
The folder App_Images has been given Read/write Permission and inherits to child folders namely myfolder1, myfolder2, myfolder3, myfolder4. Even when I tried to forcefully delete the image eKuK2511.png from the FTP File manager it is showing the following error:
550 The process cannot access the file because it is being used by another process.
How to getrid of this error?
Edit:
Upload Code:
public void UploadImages()
{
if (ServerSideValidation() == true)
{
string SavePath;
ImgPaths = new List<string>();
// Get the HttpFileCollection
HttpFileCollection hfc = Request.Files;
if (hfc.Count > 0)
{
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
#region trials compression.
SavePath = "~/App_Images/" + Session["AppContext"].ToString() + "/" + GetUniqueKey() + GetFileExtension(hpf.FileName);
SaveImageByCompressing(hpf, SavePath);
#endregion
//SavePath can be saved in DB.
ImgPaths.Add(SavePath);
//Save Thumbnail Image.
if (i == 0)
{
string savedName = "Thumb_" + GetUniqueKey() + GetFileExtension(AppDomain.CurrentDomain.BaseDirectory + ImgPaths[0].ToString().Replace("~/", "\\").Replace("/", "\\"));
SavePath = "~/App_Images/" + Session["AppContext"].ToString() + "/" + savedName;
SaveThumbImage(AppDomain.CurrentDomain.BaseDirectory + ImgPaths[0].ToString().Replace("~/", "").Replace("/", "\\"), AppDomain.CurrentDomain.BaseDirectory + "App_Images\\" + Session["AppContext"].ToString() + "\\" + savedName, 75, 75);
ImgPaths.Add(SavePath);
}
}
}
Session.Remove("AppContext");
lblMsg.Text = "Images Uploaded Successfully.";
//ShowUploadedImages(ImgPaths);
}
else
{
lblMsg.Text = "Images uploaded are either in wrong format or were deleted after uploading.";
}
}
else
{
lstPaths = new List<string>();
lblMsg.Text = "No Images Uploaded";
}
}
private void SaveImageByCompressing(HttpPostedFile hpf, string filePath)
{
Image imgFromClient = Image.FromStream(hpf.InputStream);
string SavetoFullPath = AppDomain.CurrentDomain.BaseDirectory + filePath.Replace("~/", "").Replace("/", "\\");
Image.GetThumbnailImageAbort myCallbackCompressed = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image imageToSave= imgFromClient.GetThumbnailImage(imgFromClient.Width, imgFromClient.Height, myCallbackCompressed, IntPtr.Zero);
imageToSave.Save(SavetoFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
public static void SaveThumbImage(string imagePath, string filePath, int width = 0, int height = 0)
{
Image originalImage = Image.FromFile(imagePath);
if (width > 0 && height > 0)
{
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image imageToSave = originalImage.GetThumbnailImage(width, height, myCallback, IntPtr.Zero);
imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private static bool ThumbnailCallback() { return false; }
private bool ServerSideValidation()
{
string errorMsg = string.Empty, temp = null;
bool errorFlag = true;
// Get the HttpFileCollection
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0 && hpf.FileName!=String.Empty)
{
temp = ValidateImage(hpf);
if (temp != null)
{
errorMsg += GetFileName(hpf.FileName.ToString()) + " has error : " + temp;
temp = null;
}
}
else
{
return false;
}
}
if (!string.IsNullOrWhiteSpace(errorMsg))
{
lblMsg.Text = errorMsg;
errorFlag = false;
}
return errorFlag;
}
private string GetFileExtension(string filePath)
{
FileInfo fi = new FileInfo(filePath);
return fi.Extension;
}
private string GetFileName(string filePath)
{
FileInfo fi = new FileInfo(filePath);
return fi.Name;
}
private string GetUniqueKey()
{
int maxSize = 8;
char[] chars = new char[62];
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return Session["AppContext"].ToString() + Page.User.Identity.Name.ToString() + result.ToString();
}
private string ValidateImage(HttpPostedFile myFile)
{
string msg = null;
//6MB
int FileMaxSize = 6291456;
//Check Length of File is Valid or Not.
if (myFile.ContentLength > FileMaxSize)
{
msg = msg + "File Size is Too Large. You are allowed only a maximum of 6MB per Image.";
}
//Check File Type is Valid or Not.
if (!IsValidFile(myFile.FileName))
{
msg = msg + "Invalid File Type.";
}
return msg;
}
private bool IsValidFile(string filePath)
{
bool isValid = false;
string[] fileExtensions = { ".BMP", ".JPG", ".PNG", ".GIF", ".JPEG" };
for (int i = 0; i < fileExtensions.Length; i++)
{
if (filePath.ToUpper().Contains(fileExtensions[i]))
{
isValid = true; break;
}
}
return isValid;
}
Delete Code:
/// <summary>
/// Delete all images. If there are no Images then by default NoImage.png is assigned. So skip deleting that image.
/// </summary>
/// <param name="dt"></param>
protected void DeleteApp_ImagesById(DataTable dt)
{
if (dt.Rows[0][0].ToString() != "~/images/NoImage.png")
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Rows[0][i].ToString() != string.Empty)
{
string str = Regex.Replace(dt.Rows[0][i].ToString(), "~/", "");
File.Delete(Request.PhysicalApplicationPath.ToString() + Regex.Replace(str, "/", "\\").ToString());
}
}
}
}
The docs on MSDN about Image.Save say that the file remains locked until the Image is disposed.
So I suppose that changing the code that save the images in this way
private void SaveImageByCompressing(HttpPostedFile hpf, string filePath)
{
using(Image imgFromClient = Image.FromStream(hpf.InputStream))
{
string SavetoFullPath = AppDomain.CurrentDomain.BaseDirectory +
filePath.Replace("~/", "").Replace("/", "\\");
Image.GetThumbnailImageAbort myCallbackCompressed =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
using(Image imageToSave= imgFromClient.GetThumbnailImage(imgFromClient.Width,
imgFromClient.Height, myCallbackCompressed, IntPtr.Zero))
{
imageToSave.Save(SavetoFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public static void SaveThumbImage(string imagePath, string filePath,
int width = 0, int height = 0)
{
using(Image originalImage = Image.FromFile(imagePath))
{
if (width > 0 && height > 0)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
using(Image imageToSave = originalImage.GetThumbnailImage(width, height,
myCallback, IntPtr.Zero))
{
imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else
{
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
is a safe way to ensure your images are immediately unlocked when you finish with the upload code
i have imgae path in datatable[id, path] now this would a value like
ex: id path
1 F:\R&D\RD\RD\Images\a1.JPG;
2 F:\R&D\RD\RD\Images\a2.JPG;
3 F:\R&D\RD\RD\Images\a3.JPG;
now these images are in size width*height (1018*768). now i need to convert these images into thumnail
caling the function
**C_Thumbnails(100, "F:\R&D\RD\RD\Images\a1.JPG", "F:\R&D\RD\RD\Images]thum.jpg")**
public static void C_Thumbnails(int size, string FilePath, string ThumbPath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(FilePath);
try
{
int thumbHeight, thumbWidth;
decimal h = image.Height;
decimal w = image.Width;
if (image.Height > image.Width)
{
thumbHeight = size;
decimal tWidth = (w / h) * thumbHeight;
thumbWidth = Convert.ToInt32(tWidth);
}
else
{
thumbWidth = size;
decimal tHeight = (h / w) * thumbWidth;
thumbHeight = Convert.ToInt32(tHeight);
}
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
image.Dispose();
thumbnailImage.Save(ThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
image.Dispose();
throw ex;
}
}
like this i am coverting into thumbnail image. but here i am saving the thumbnail image under the path F:\R&D\RD\RD\Images\thum.jpg .
so is there any way without saving the thumbnail in the disk and how to bind the new thumbnail image in the repeater control and i need to show image there. but if once user clicks on the thumnail image a orignal image should pop up.
if any one have did this functionlity any where let me know
working on this solution from past 2 days.
any help would be greatly appreciated
now after changing code
like this
public void ProcessRequest(HttpContext context)
{
string imageid = context.Request.Params["ImageID"];
string thumbnail = context.Request.Params["thumbnail"];
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand("select Image from Image where ImageID=" + imageid, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
string filePath = dr["image"].ToString();
dr.Close();
if (!System.IO.File.Exists(filePath))
{
//you have a problem
return;
}
if (context.Request.Params["thumbnail"] == "true")
{
//TODO: the thumbnail
// Image thumbnailImage = originalImage.GetThumbnailImage to generate thumbnail then
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "image/" + format;
thumbnailImage.Save(Response.OutputStream, imageFormat);
thumbnailImage.Dispose();
}
else
{ //stream directly the image fromdisk
System.IO.FileStream fs = System.IO.File.OpenRead(filepath);
const int ChunkSize = 10000;
Byte[] buffer = new Byte[ChunkSize];
long dataLengthToRead = fs.Length;
while (dataLengthToRead > 0)
{
int lengthRead = fs.Read(buffer, 0, ChunkSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
System.Web.HttpContext.Current.Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
fs.Close();
}
}
}
in repeater control i have added this line of code
','_blank','toolbar=no,menubar=no'))" > '/>
my thumbnail image is not displayed still but once i click my thumbnail image i am able to see the entire image in new pop up
You should use a Photo Image Handler to serve the pages from your disk to the clients.
Then in the repeater control
<a href="ImageHandler.ashx?thumbnail=false&id='<%# Eval("ID")%>'>
<img src="ImageHandler.ashx?thumbnail=true&id='<%# Eval("ID")%>' border='0'/>
</a>
The ideea is not to pass in the actual path/name of the file, but the ID of the item you wish to view. Then the handler will:
public void ProcessRequest(System.Web.HttpContext context)
{
string filePath = //TODO: Get File Path from ItemID = context.Request.Params["id"]
if (!System.IO.File.Exists(filePath))
{
//you have a problem
return;
}
if(context.Request.Params["thumbnail"]=="true")
{
//TODO: the thumbnail
// Image thumbnailImage = originalImage.GetThumbnailImage to generate thumbnail then
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "image/" + format;
thumbnailImage.Save(Response.OutputStream, imageFormat);
thumbnailImage.Dispose();
} else
{ //stream directly the image fromdisk
System.IO.FileStream fs = System.IO.File.OpenRead(filepath);
const int ChunkSize = 10000;
Byte[] buffer = new Byte[ChunkSize];
long dataLengthToRead = fs.Length;
while (dataLengthToRead > 0)
{
int lengthRead = fs.Read(buffer, 0, ChunkSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
System.Web.HttpContext.Current.Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
fs.Close();
}
}
I hwas trying to generate a multi page XPS document from a web application and trying to stream that ona button click.
public class Class1
{
protected void btnGenerateLetter_OnClick(object sender, EventArgs e)
{
try
{
string sid = Request.Form["id"];
byte[] bytes = FlowDocumentToXPS(GenerateLetter(), 640, 800);
Response.Clear();
Response.ContentType = "application/vnd.ms-xpsdocument";
Response.AddHeader("Content-Disposition", "attachment; filename=document.xps");
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
}
}
private FlowDocument GenerateLetter()
{
FlowDocument flowDocument = new FlowDocument();
string Header = "Test Header Message";
string Body = "Content goes here";
string Footer = "Footer Text";
for (int i = 0; i < 3; i++)
{
Paragraph header = new Paragraph();
header.Margin = new System.Windows.Thickness(250, 100, 250, 10);
header.BreakPageBefore = true;
header.Inlines.Add(new Run(Header));
header.Inlines.Add(new LineBreak());
header.Inlines.Add(new LineBreak());
header.Inlines.Add(new LineBreak());
Paragraph body = new Paragraph();
body.Inlines.Add(new Run(Body));
body.Inlines.Add(new LineBreak());
body.Inlines.Add(new LineBreak());
Paragraph footer = new Paragraph();
footer.Inlines.Add(new Run(Footer));
flowDocument.Blocks.Add(header);
flowDocument.Blocks.Add(body);
flowDocument.Blocks.Add(footer);
}
return flowDocument;
}
public static byte[] FlowDocumentToXPS(FlowDocument flowDocument, int width, int height)
{
MemoryStream stream = new MemoryStream();
// create a package
using (Package package = Package.Open(stream, FileMode.CreateNew))
{
// create an empty XPS document
using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed))
{
// create a serialization manager
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
// retrieve document paginator
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
// set page size
paginator.PageSize = new System.Windows.Size(width, height);
// save as XPS
rsm.SaveAsXaml(paginator);
rsm.Commit();
}
return stream.ToArray();
}
}
}
This wroks fine on the development environment.But getting this error when deployed on a different machine.(IIS6).
Startup URI: C:\Documents and Settings\050583b.syn\Desktop\document.xps
Application Identity:
System.IO.FileFormatException: File contains corrupted data.
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream)
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
at MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock()
at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream)
at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream)
at MS.Internal.Documents.Application.TransactionalPackage..ctor(Stream original)
at MS.Internal.Documents.Application.PackageController.MS.Internal.Documents.Application.IDocumentController.Open(Document document)
at MS.Internal.Documents.Application.DocumentManager.DispatchOpen(IDocumentController controller, Document document)
at MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.b__5(IDocumentController controller, Document subject)
at MS.Internal.Documents.Application.ChainOfResponsiblity2.Dispatch(Action action, S subject)
at MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.<OrderByLeastDependent>b__4(Document member)
at MS.Internal.Documents.Application.ChainOfDependencies1.OrderByLeastDependent(T member, Action action)
at MS.Internal.Documents.Application.DocumentManager.OrderByLeastDependent(DispatchDelegate action, Document document)
at MS.Internal.Documents.Application.DocumentManager.Open(Document document)
at MS.Internal.AppModel.ApplicationProxyInternal.InitContainer()
at MS.Internal.AppModel.ApplicationProxyInternal.Run(InitData initData)
I guess the problem is the with the bytes not completely getting written into the response.
Try the following and hopefully it should work.
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ContentType = "application/vnd.ms-xpsdocument";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=document.xps");
context.Response.End();
I have an aspx page which will upload images to server harddisk from client pc
But now i need to change my program in such a way that it would allow me to resize the image while uploading.
Does anyone has any idea on this ? I couldnt not find such properties/methods with Input file server control
Any one there to guide me ?
Once the file has been saved to the server you can use code like this to resize. This code will take care of length/width ratio on the resize.
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
return loBMP;
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
You will not be able to resize "on the fly" since you will need to have the full image before you perform any image transformations. However, after the upload is complete and before you display any results to your user, you can use this basic image resizing method that I've used in a couple of my apps now:
''' <summary>
''' Resize image with GDI+ so that image is nice and clear with required size.
''' </summary>
''' <param name="SourceImage">Image to resize</param>
''' <param name="NewHeight">New height to resize to.</param>
''' <param name="NewWidth">New width to resize to.</param>
''' <returns>Image object resized to new dimensions.</returns>
''' <remarks></remarks>
Public Shared Function ImageResize(ByVal SourceImage As Image, ByVal NewHeight As Int32, ByVal NewWidth As Int32) As Image
Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(NewWidth, NewHeight, SourceImage.PixelFormat)
If bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format1bppIndexed Or _
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format4bppIndexed Or _
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format8bppIndexed Or _
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Undefined Or _
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.DontCare Or _
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppArgb1555 Or _
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppGrayScale Then
Throw New NotSupportedException("Pixel format of the image is not supported.")
End If
Dim graphicsImage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
graphicsImage.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
graphicsImage.DrawImage(SourceImage, 0, 0, bitmap.Width, bitmap.Height)
graphicsImage.Dispose()
Return bitmap
End Function
Another approach would to allow the user to adjust the size in the browser and then resize the image as described in other answers.
So take a look at this solution which allows you to upload and crop images with jQuery, jCrop & ASP.NET.
How to resize & Upload Image only for .jpg Extensions :
In upload.aspx page
<asp:FileUpload ID="ProductImage" runat="server"/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" />
<asp:TextBox runat="server" ID="txtProductName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtProductName" ErrorMessage="The Product name field is required." />
And upload.aspx.cs
For resize
/// <summary>
/// Created By Rajib Chowdhury Mob. 01766-306306; Web: http://onlineshoping.somee.com/
/// Complete This Page Coding On January 05, 2014
/// Programing C# By Visual Studio 2013 For Web
/// Dot Net Version 4.5
/// Database Virsion MSSQL Server 2005
/// </summary>
public bool ResizeImageAndUpload(System.IO.FileStream newFile, string folderPathAndFilenameNoExtension, double maxHeight, double maxWidth)
{
try
{
// Declare variable for the conversion
float ratio;
// Create variable to hold the image
System.Drawing.Image thisImage = System.Drawing.Image.FromStream(newFile);
// Get height and width of current image
int width = (int)thisImage.Width;
int height = (int)thisImage.Height;
// Ratio and conversion for new size
if (width > maxWidth)
{
ratio = (float)width / (float)maxWidth;
width = (int)(width / ratio);
height = (int)(height / ratio);
}
// Ratio and conversion for new size
if (height > maxHeight)
{
ratio = (float)height / (float)maxHeight;
height = (int)(height / ratio);
width = (int)(width / ratio);
}
// Create "blank" image for drawing new image
Bitmap outImage = new Bitmap(width, height);
Graphics outGraphics = Graphics.FromImage(outImage);
SolidBrush sb = new SolidBrush(System.Drawing.Color.White);
// Fill "blank" with new sized image
outGraphics.FillRectangle(sb, 0, 0, outImage.Width, outImage.Height);
outGraphics.DrawImage(thisImage, 0, 0, outImage.Width, outImage.Height);
sb.Dispose();
outGraphics.Dispose();
thisImage.Dispose();
// Save new image as jpg
outImage.Save(Server.MapPath(folderPathAndFilenameNoExtension + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
outImage.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
And Button1_Click Event
string filePath = "~\\Image\\";//your normal image path
if (Page.IsValid)
{
HttpPostedFile myFile = ProductImage.PostedFile;//Get Slected Image
int nFileLen = myFile.ContentLength;//Get slected Image Size
string myimag = txtProductName.Text;//Get user input image name
Guid ImageName = Guid.NewGuid();//get unique id
if ((myFile != null) && (nFileLen > 1048576))
{
LabelAddStatus.Text = "minimum size exceed"; //If file image size 1 MB above
}
else
{
try
{
if (ProductImage.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(ProductImage.FileName).ToLower();
String[] allowedExtensions = { ".jpg" };//Declare For Allowed Extension
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
// Read file into a data stream
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
myFile.InputStream.Dispose();
// Save the stream to disk as temporary file. make sure the path is unique!
System.IO.FileStream newFile
= new System.IO.FileStream(Server.MapPath(filePath + "_temp.jpg"),
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
bool success = ResizeImageAndUpload(newFile, filePath + ("thumbs"+myimag + ImageName), 100, 100);//Save image your thumb image path
success = ResizeImageAndUpload(newFile, filePath + (myimag + ImageName), 768, 1024);//Save image your normal image path
//delete the temp file.
newFile.Close();
System.IO.File.Delete(Server.MapPath(filePath + "_temp.jpg"));
LabelAddStatus.Text = "File uploaded.";
}
else
{
LabelAddStatus.Text = "Unable to accept file type..";
}
}
}
}
catch (Exception)
{
//No Exception Message
}
}
}
Thanks...
This is how I did in my project, based on your condition (height/width) you can change the parameter ie(MaxHeight)
public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)
{
var ratio = (double)maxHeight / image.Height;
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
On Button click:
protected void Button1_Click(object sender, EventArgs e)
{
lblmsg.Text="";
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
Guid uid = Guid.NewGuid();
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("LogoImagesFolder") + "\\" + uid+fn;
try
{
string fileExtention = File1.PostedFile.ContentType;
int fileLenght = File1.PostedFile.ContentLength;
if (fileExtention == "image/png" || fileExtention == "image/jpeg" || fileExtention == "image/x-png")
{
if (fileLenght <= 1048576)
{
System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream);
System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81);
objImage.Save(SaveLocation,ImageFormat.Png);
lblmsg.Text = "The file has been uploaded.";
lblmsg.Style.Add("Color", "Green");
}
else
{
lblmsg.Text = "Image size cannot be more then 1 MB.";
lblmsg.Style.Add("Color", "Red");
}
}
else {
lblmsg.Text = "Invaild Format!";
lblmsg.Style.Add("Color", "Red");
}
}
catch (Exception ex)
{
lblmsg.Text= "Error: " + ex.Message;
lblmsg.Style.Add("Color", "Red");
}
}
}
You'll need to use the WebClient class to download the remote image.
After that, then you can resize it...Use DrawImage, not GetThumbnailImage. Make sure you dispose of your bitmap and graphics handles.. (use using{}). Set all quality settings to high.
You might want to take a look at the source code for my popular image resizer first... It will help you avoid some common trouble areas.
//Here is another WAY fox!!! i have actually modify the code from You all. HIHI
//First, add one textBox and one FileUpload Control, and a button
//paste this in your code behind file... after public partial class admin : System.Web.UI.Page
string OriPath;
string ImageName;
public Size NewImageSize(int OriginalHeight, int OriginalWidth, double FormatSize)
{
Size NewSize;
double tempval;
if (OriginalHeight > FormatSize && OriginalWidth > FormatSize)
{
if (OriginalHeight > OriginalWidth)
tempval = FormatSize / Convert.ToDouble(OriginalHeight);
else
tempval = FormatSize / Convert.ToDouble(OriginalWidth);
NewSize = new Size(Convert.ToInt32(tempval * OriginalWidth), Convert.ToInt32(tempval * OriginalHeight));
}
else
NewSize = new Size(OriginalWidth, OriginalHeight); return NewSize;
}
//Now, On Button click add the folwing code.
if (FileUpload1.PostedFile != null)
{
ImageName = TextBox1.Text+".jpg";
OriPath = Server.MapPath("pix\\") + ImageName;
//Gets the Full Path using Filecontrol1 which points to actual location in the hardisk :)
using (System.Drawing.Image Img = System.Drawing.Image.FromFile(System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName)))
{
Size ThumbNailSize = NewImageSize(Img.Height, Img.Width, 800);
using (System.Drawing.Image ImgThnail = new Bitmap(Img, ThumbNailSize.Width, ThumbNailSize.Height))
{
ImgThnail.Save(OriPath, Img.RawFormat);
ImgThnail.Dispose();
}
Img.Dispose();
}
}
//Enjoy. If any problem,, mail me at izacmail#gmail.com
To resize down a image and get smaller sizes just make the changes below
bmpOut = new Bitmap(lnNewWidth, lnNewHeight, **System.Drawing.Imaging.PixelFormat.Format24bppRgb**);
Graphics g = Graphics.FromImage(bmpOut);
as you above a set the imagem to Format24bppRgb PixelFormat.
and when you save the file, you set the ImageFormat also. Like this:
bmpOut.Save(PathImage, System.Drawing.Imaging.ImageFormat.Jpeg);
You can use this, it does a dandy job for me. But it does not handle low res images well for me. Thankfully I down use to many of them. Just sent it the image byte[] and the expected output and you'll be good to go.
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb))
{
newImage.SetResolution(oldImage.HorizontalResolution, oldImage.VerticalResolution);
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(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.Width > oldSize.Height)
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * (float)targetSize / (float)oldSize.Width);
}
else
{
newSize.Width = (int)(oldSize.Width * (float)targetSize / (float)oldSize.Height);
newSize.Height = targetSize;
}
return newSize;
}
You could resize before sending to the server using an ActiveX control. There is a free ASP.net image uploading component (I believe this is the same one that Facebook actually uses) available here:
http://forums.aurigma.com/yaf_postst2145_Image-Uploader-ASPNET-Control.aspx
Let me know if it works, I am thinking about implementing it in my projects here at work.
Edit: Looks like the wrapper for the object is free, however the actual component itself is going to run you about $200. I confirmed it is the same component Facebook is using though.
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class admin_AddPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string reportPath = Server.MapPath("../picnic");
if (!Directory.Exists(reportPath))
{
Directory.CreateDirectory(Server.MapPath("../picnic"));
}
}
protected void PhotoForm_ItemInserting(object sender, FormViewInsertEventArgs e)
{
FormView uploadForm = sender as FormView;
FileUpload uploadedFile = uploadForm.FindControl("uploadedFile") as FileUpload;
if (uploadedFile != null)
{
string fileName = uploadedFile.PostedFile.FileName;
string pathFile = System.IO.Path.GetFileName(fileName);
try
{
uploadedFile.SaveAs(Server.MapPath("../picnic/") + pathFile);
}
catch (Exception exp)
{
//catch exception here
}
try
{
Bitmap uploadedimage = new Bitmap(uploadedFile.PostedFile.InputStream);
e.Values["ImageWidth"] = uploadedimage.Width.ToString();
e.Values["ImageHeight"] = uploadedimage.Height.ToString();
// Make output File Name
char[] splitter = { '.' };
string[] splitFile = pathFile.Split(splitter);
string OutputFilename = splitFile[0] + "s";
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbImage = uploadedimage.GetThumbnailImage(74, 54, myCallback, IntPtr.Zero);
thumbImage.Save(Server.MapPath("../picnic/") + OutputFilename + ".jpg");
e.Values["Thumbnail"] = "./picnic/" + OutputFilename + ".jpg";
}
catch (Exception ex)
{
//catch exception here
}
e.Values["Pic"] = "./picnic/" + pathFile;
e.Values["Url"] = "./picnic/" + pathFile;
e.Values["dateEntered"] = DateTime.Now.ToString();
}
}
public bool ThumbnailCallback()
{
return false;
}
}
This uses a FileUpload and a FormView to insert. Then I use the GetThumnailImage() method provided in System.Drawing.Imaging. You can enter any Width and Height values and it will shrink/stretch accordingly.
uploadedimage.GetThumbnailImage(W, H, myCallback, IntPtr.Zero);
Hope this helps.
The uploading of the image file is performed by ASP.NET 4.0 Client Callbacks. If you are not familiar with client callbacks then I suggest that you take a look at ASP.Net AJAX Control Toolkit AsyncFileUpload Control without page refresh or PostBack in ASP.Net Web Page or ASP.Net AJAX Update Panel. The callback is fired as soon as the file is selected by the user using the file field control.
public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath)
{
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
double widthRatio = (double)fullSizeImg.Width / (double)Width;
double heightRatio = (double)fullSizeImg.Height / (double)Height;
double ratio = Math.Max(widthRatio, heightRatio);
int newWidth = (int)(fullSizeImg.Width / ratio);
int newHeight = (int)(fullSizeImg.Height / ratio);
//System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
//DateTime MyDate = DateTime.Now;
//String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf("."));
thumbNailImg.Save(destPath, ImageFormat.Jpeg);
thumbNailImg.Dispose();
return "";
}
public bool ThumbnailCallback() { return false; }
private void ResizeImage(FileUpload fileUpload)
{
// First we check to see if the user has selected a file
if (fileUpload.HasFile)
{
// Find the fileUpload control
string filename = fileUpload.FileName;
// Check if the directory we want the image uploaded to actually exists or not
if (!Directory.Exists(MapPath(#"Uploaded-Files")))
{
// If it doesn't then we just create it before going any further
Directory.CreateDirectory(MapPath(#"Uploaded-Files"));
}
// Specify the upload directory
string directory = Server.MapPath(#"Uploaded-Files\");
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(fileUpload.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 100;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(directory + "tn_" + filename);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
// Write a message to inform the user all is OK
label.Text = "File Name: <b style='color: red;'>" + filename + "</b><br>";
label.Text += "Content Type: <b style='color: red;'>" + fileUpload.PostedFile.ContentType + "</b><br>";
label.Text += "File Size: <b style='color: red;'>" + fileUpload.PostedFile.ContentLength.ToString() + "</b>";
// Display the image to the user
Image1.Visible = true;
Image1.ImageUrl = #"Uploaded-Files/tn_" + filename;
}
else
{
label.Text = "No file uploaded!";
}
}