|
That is perfect
|
|
|
|
|
Unless I'm missing sometime; this will lose the transparency layer of a png or gif format; is there a fix for this?
Lessons learned from 911:
1. United We Stand.
2. United’s We Fall.
Gulf War Syndrome survivors never have a good day. http://www.vetshelpcenter.com/
|
|
|
|
|
Hi dude,
I just modified ur code for my project..it is very helpful for me now...Thanks a lot....
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
If FileUpload1.HasFile Then
strFile = System.IO.Path.GetFileName(FileUpload1.FileName)
filename = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName)
strpath = "F:\testpic\"
thbpath = "thumbs" & "\" & filename + ".png"
FileUpload1.SaveAs(strpath & strFile)
ImageResize()
End If
Catch ex As Exception
lbl_msg.Text = ex.Message
End Try
End Sub
Public Function ImageResize() As System.Web.UI.WebControls.Image
Dim bm As New Bitmap(strpath & strFile)
Dim bmn As New Bitmap(Image.FromHbitmap(bm.GetHbitmap), width, CInt(Math.Round(width * (bm.Height / bm.Width), 0)))
Dim img As System.Drawing.Image = Image.FromHbitmap(bmn.GetHbitmap)
Dim g As Graphics
g = Graphics.FromImage(bmn)
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
g.DrawImage(bmn, 0, 0, 100, 100)
Try
img.Save(strpath & "\" & thbpath)
Dim img_new As New System.Web.UI.WebControls.Image()
Return img_new
Catch ex As Exception
lbl_msg.Text = ex.Message
Return Nothing
End Try
End Function
I am a .Net profesional working...in a company...in India..
|
|
|
|
|
hi, Hi thanks i also had the same problem. I would now like to save the resized image to a database then delete the temporary uploaded file.
|
|
|
|
|
Hello,
you'll get this error:
The call is ambiguous between the following methods or properties: 'System.Math.Ceiling(decimal)' and 'System.Math.Ceiling(double)'
On line:
<br />
Int32 new_height = (Int32) Math.Ceiling( (old_height * new_width)/old_width );<br />
to supress the error do this:
<br />
Int32 new_height = (Int32)Math.Ceiling(((double)old_height * (double)new_width) / (double)old_width);<br />
|
|
|
|
|
This worked great for me. Thanks for posting!
|
|
|
|
|
Sir,
I am doing displaying and downloading of audio files in asp.net.
I am displaying records using datagrid and bounded the data with data set.
I've placed those audio file names in data base and files are in virtual directory.
Now my problem is how to download a specific file?
So please kindly give me the solution
Thanking you,
Sravani
|
|
|
|
|
Hi,
I need more detail on how to make your code works.
I don't know where to find web.config file?
here is what I have done so far. I created a folder on the server called PICS than I placed PNGUploader.aspx in the folder.
I was able to run it fine I clicked on browse buton to select an
image than I set the size width and height to 2.
when I hit on upload button It gave me a message that says:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customerrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customerrors> tag should then have its "mode" attribute set to "Off".
<configuration>
<system.web>
<customerrors mode="Off">
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customerrors> configuration tag to point to a custom error page URL.
<configuration>
<system.web>
<customerrors mode="RemoteOnly" defaultredirect="mycustompage.htm">
I don't know where to find web.config file?
The idea of my project is to let the the user upload an image BMP, GIF, JPG, JFIF, JPEG, or PNG format resize the image than store it in a folder where I can retrive from mysql database.
I never done this but based on my research I would have to write a path in mysql database to point to the right image.
Thanks,
lulu
|
|
|
|
|
Thanks for this great article!
I have found several good articles about how to resize images and/or generate thumbnails using ASP.NET (C# or VB.NET) such as this one.
But what about image resampling? What I'm trying to do is replace the functionality of the ASPimage ASP component that I've used in the past to do 3 things. 1) Resample images to 72dpi, then 2) Resize to acceptable dimensions, then 3) Generate a thumbnail. Take a photo album web application for example. Many users are not savvy about optimizing images for use on the web. So they may upload a 1MB 300dpi photo into their album. Well, I, the host, pay for that space and I want a good download experience for the site visitors. So, I've used ASPImage to check the dpi--if greater than 72, resample to 72dpi. Then if image width is greater than say 700pixels, resize to 700pixels wide while maintaining aspect ratio. Lastly, I'd use the ASPImage component to create a 100pixel wide thumbnail again maintaining aspect ratio of the original image.
So now I'm trying to duplicate this functionality using .NET and without third-party components. What I'm missing is how to know current dpi of an image and then how to resample it. Does this exist in the .NET Library?
|
|
|
|
|
Hi,
U can acces resolution of Image by its horizontal and vertical resolution properties, but these properies are read only.
public float HorizontalResolution { get; }
Member of System.Drawing.Image
public float VerticalResolution { get; }
Member of System.Drawing.Image
Try to resave images in JPEG or PNG at runtime, they all are 72dpi formats as I know.
|
|
|
|
|
Hi there, just want to say, what a fantastic solution to this problem. It works fantastically. However, I am having a similiar problem. Any files with resolutions over 72dpi won't upload. However if I take an image with my phone it seems to be 96dpi and won't upload it. Is there a way around this. I also have a site where the users may not be image manipulation savvy and need to put something in place that automatically change the DPI of the image to 72dpi regardless of the original image dpi.
I also assumed that all JPG images were 72dpi, it seems that new phones and cameras are saving the images out at 96dpi -strange but seemingly true.
Hope you can help.
thanks in advance - Pete.
|
|
|
|
|
Call me stoopid, but whenever i run this (even after taking inheritable permissions off the virtual directory folder and giving full permissions to everyone, AND local ASPNET account AND domain ASPNET account i still get a username and password box asking 4 credentials.
What am i doing wrong here?
---Guy H ( ---
|
|
|
|
|
Have you checked your IIS security settings for the directory?
Ensure that only the Anonymous Access option is selected withing the authentication controls of the directory security...
|
|
|
|
|
First of all, thanks a million to DotNut for the great article. You saved me a ton of time. I thought I'd share a simple bit of code I based on your example that scales an image to fit within a specified height and width. I found this useful where I have users submitting images, and I need to insure that they will fit nicely into my HTML later.
Here it is. I hope somebody finds it as useful as I found your article.
Thanks again-
chlock
<br />
private System.Drawing.Image ScaleImage(int maxHeight, int maxWidth, System.Drawing.Image image)<br />
{<br />
double origImgHeight = image.Height;<br />
double origImgWidth = image.Width;<br />
double scaledImgWidth;<br />
double scaledImgHeight;<br />
<br />
if(origImgHeight > origImgWidth)<br />
{<br />
scaledImgHeight = maxHeight;<br />
scaledImgWidth = (maxHeight / origImgHeight) * origImgWidth;<br />
}<br />
else<br />
{<br />
scaledImgWidth = maxWidth;<br />
scaledImgHeight = (maxWidth / origImgWidth) * origImgHeight;<br />
}<br />
<br />
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack =<br />
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);<br />
<br />
return image.GetThumbnailImage(Convert.ToInt16(scaledImgWidth), Convert.ToInt16(scaledImgHeight), dummyCallBack, IntPtr.Zero);<br />
<br />
}<br />
And here's an example of using it in it's intended context:
<br />
private void btnUpload_Click(object sender, System.EventArgs e)<br />
{<br />
string errMsg = ValidateFile(txtFileName.PostedFile);<br />
<br />
if(errMsg == String.Empty)<br />
{ <br />
System.Web.HttpContext context = System.Web.HttpContext.Current;<br />
<br />
if (context.User.Identity.Name != "")<br />
{<br />
string strBaseLocation = Server.MapPath(".\\UploadPics");<br />
string strFileNameOnServer = context.User.Identity.Name + ".tmp";<br />
string tempFilePath = strBaseLocation + "\\" + strFileNameOnServer;<br />
string thumbSaveName = context.User.Identity.Name + "~" + DateTime.Now.ToString("ddMMyyhhmmss") + ".png";<br />
string thumbSavePath = strBaseLocation + "\\" + thumbSaveName ;<br />
<br />
try <br />
{ <br />
txtFileName.PostedFile.SaveAs(tempFilePath);<br />
lblMessage.Text = "File uploaded successfully."; <br />
}<br />
catch(Exception ex)<br />
{<br />
lblMessage.Text = "Error saving file.\n" + ex.Message;<br />
} <br />
<br />
try<br />
{ <br />
string imageUrl = "UploadPics/" + strFileNameOnServer;<br />
<br />
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl)); <br />
System.Drawing.Image thumbNailImg = ScaleImage(150,120,fullSizeImg);<br />
<br />
thumbNailImg.Save (thumbSavePath, ImageFormat.Png);<br />
<br />
thumbNailImg.Dispose();<br />
fullSizeImg.Dispose();<br />
<br />
long fileSize = File.OpenRead(thumbSavePath).Length / 1000;<br />
if(fileSize > 150)<br />
{<br />
lblMessage.Text = "File too big.";<br />
}<br />
else<br />
{<br />
Image1.AlternateText = "Resized image"; <br />
Image1.ImageUrl = "UploadPics\\" + thumbSaveName;<br />
}<br />
<br />
try<br />
{<br />
if(File.Exists(tempFilePath))<br />
{<br />
File.Delete(tempFilePath); <br />
}<br />
}<br />
catch(Exception ex)<br />
{<br />
lblMessage.Text = ex.Message;<br />
}<br />
<br />
}<br />
catch(Exception ex)<br />
{<br />
Response.Write("An error occurred - " + ex.ToString());<br />
} <br />
}<br />
}<br />
else<br />
{<br />
lblMessage.Text = errMsg;<br />
}<br />
}<br />
<br />
public bool ThumbnailCallback()<br />
{<br />
return false;<br />
}<br />
<br />
private string ValidateFile(HttpPostedFile file)<br />
{<br />
string fileName = txtFileName.PostedFile.FileName; <br />
<br />
string ext = GetFileExt(fileName);<br />
if(ext == String.Empty)<br />
{ <br />
return "Sorry, supported file types are GIF, BMP, JPG, and PNG.";<br />
}<br />
<br />
return String.Empty;<br />
}<br />
<br />
private string GetFileExt(string fileName)<br />
{<br />
string ext = fileName.Substring(fileName.Length - 4).ToUpper();<br />
<br />
switch(ext)<br />
{<br />
case ".JPG":<br />
return ext; <br />
<br />
case ".GIF":<br />
return ext; <br />
<br />
case ".BMP":<br />
return ext; <br />
<br />
default:<br />
return String.Empty;<br />
}<br />
}<br />
|
|
|
|
|
Unfortunatly, the base article blew it when it came to resizing images..
DO NOT USE THIS METHOD FOR RESIZING IMAGES IN .NET!!
If you have images that are submitted by users and one of them takes a submits a .Jpg file with a embedded thumbnail, the code will not function.
Well, it will show a thumbnail that will look bad until you return the full image.
This is because the function 'image.GetThumbnailImage' is DESIGNED to extract out the thumbnail image from a image - NOT to scale the original image to create a thumbnail. (Which is exactly what it says in the help!)
(take an Image from a Minolta 7i or 7hi camera to get a good example of a .jpg with an embedded thumbnail)
So, here is what happens.. I start off with an image that is 2560x1920 that looks good, I call image.GetThumbnailImage(50,...) to get a small image that is 50 pixels wide.. it looks pretty good...
But if I call image.GetThumbnailImage(400,...) it looks like total crud - that is because it took the small thumbnail image embedded in the .jpg (maybe 100x80 or smaller) and streched it to be 400 wide... Now each pixel is a block of about 10 pixels wide... If you make this 1024 wide you get a real mess..
So, instead of doing that, just call:
Image Img; // Original
Image Thumb = new Bitmap (Img, NewWidth, NewHeight);
And Use the new thumbnail... Not only is it correct, it is MUCH cleaner code! Goodness knows why someone wrote the interface they did on GetThumbnailImage!!!
-Chert
PS: I tried using .png files instead of .jpg files, and got wonderfuly bad results.. the pictures were just about the same appearence, but instead of being small, I got this:
6,669 PICT1134-0.jpg 50 pixels wide
52,987 PICT1134-0.png Same.
46,249 PICT1134-1.jpg 400 pixels wide
555,106 PICT1134-1.png Same. over 10X larger!!!
143,987 PICT1134-2.jpg 1024 pixels wide
2,278,688 PICT1134-2.png Same. Almost 16X larger!
So, sure it might be a little better, but the last image file is only a little smaller than my original 5 Mp image!
Doesn't that pretty much defeat the point of making it a thumbnail?
(I used the default values to save, calling save with only ImageFormat.png or .Jpeg)
-Chert
|
|
|
|
|
Hello,
Let's say that we have the same image in different filtypess like jpg, gif and png.
What can you tell me about the file size of the png file format looking to the others?
Thanks!
|
|
|
|
|
If you follow the link in the article under the "PNG WHAT IS IT?" heading, there is an article about the different file sizes it gives you a sample of the three diffeernt formats and their sizes.
For reference "The PNG format contains true color. The following examples (which can be seen by following the link) show the same image as a GIF (59 KB), JPG (27 KB), and PNG (67 KB).
Talking
|
|
|
|
|