|
|
|
|
hi , your sample so cool , but i have a question?
how can i delete main image after resizing? cause when i wanna delete
main image by : "system.io.file.delete(imgpath)" i recieve an error that
file is used!???
how can i fix it?
tanx
|
|
|
|
|
Hi,
If you read the docu about the Image.FromFile-Method, you learn that the Image will be locked until the Image-object is released.
To avoid this, I'd suggest a little helper function:
Image GetImage(string strImagePath)
{
FileStream stream = new FileStream(strImagePath, FileMode.Open);
Image image = Image.FromStream(stream);
stream.Close();
return image;
}
With this approach you should be able to solve your problem.
|
|
|
|
|
just add
fullSizeImg.Dispose();
after
thumbNailImg.Dispose();
Apurv
“Never trust a computer you can’t throw out a window.”
(Steve Wozniak)
“There are only two industries that refer to their customers as ‘users’.”
(Edward Tufte)
|
|
|
|
|
hi,
this is a useful script and i'm trying to apply it but i need to complete it by recording path and image name on my db access.
how can i do?
please reply.
daniele
|
|
|
|
|
Hi,
i have a question/problem:
when i create an Image object from a image file, into c#, how do i control the number of colors of its pallete?
Meaning, i have a PNG 8bit depth 25colors that has 26KB, but when i send it to an C# image object its size duplicates.. and its now 52KB.
How do i keep the same properties of the PNG for when i use, for example, a Image.save(...) it returns my original file ?
Thanks,
Vitor
Vitor Duarte
|
|
|
|
|
Is this C# code also available as Visual Basic code?
|
|
|
|
|
How can I adjust the code to enable overwriting of files already uploaded. I have already changed the code to not auto assign a file name on each upload.
Thanks.
|
|
|
|
|
The process cannot access the file "D:\test\pics\top.jpg" because it is being used by another process.
----------------------------------------------------------------------
FileDetails.Visible = true; //div is made visible
// Save uploaded file to server at the in the Pics folder
MyFile.PostedFile.SaveAs(Request.PhysicalApplicationPath + "pics\\" + UploadedFileName );
MyFile.Dispose();
|
|
|
|
|
aspnet_wp.exe is locking the files. I had to kill the process in order to remove the file.
|
|
|
|
|
ImageManipulator is a free open source image library I wrote up at Codeplex.
It deals with this issue by copying the image before making it available to work with. The assembly as well as a winforms demo app can be found at:
http://www.codeplex.com/imagemanipulator[^]
It would not be difficult to incorporate the png stuff from this article into that framework or vice-versa.
Project Description
Assembly for easiest possible (1)resampling, with optional jpg compression (2)cropping, and (3)history stack (undo/redo)
- Gerry
|
|
|
|
|
I've found a problem applying this article on some images. The quality after the resizing is very poor. I've found an article that explain the work of GetThumbNailImage.
Some kind of images have a thumbnail embedded inside (some kinf of camera save photo in this way) usually 32x32. When you call GetThumbNailImage to get the thumbnail, if is present the embedded thumbnail this method take the thumbnail and resize to the given width and height instead of resizing the fullsize image.
This is the problem of loosing qaulity during the resizing: you have a 32x32 image resized to your desired size.
My question is: how to determine if an image have an embedded thumbnail inside?
Edika
-- modified at 13:54 Sunday 26th February, 2006
|
|
|
|
|
thx , u did a great job
but i am asking why making dummy callBack especially it can works without it and we can just pass null to the function GetThumbnailImage instead of dummy call back
Emad
[Message text goes here]
|
|
|
|
|
The easiest way to upload and resize an image to the internet is I-Load.
I-Load is a FREE ASP.NET web control with numerous benefits and features.
You can download I-Load (it's FREE!) and view an online demo here:
http://www.radactive.com/en/Products/ILoad/Overview.aspx
|
|
|
|
|
While this looks cool, unfortunately it ain't free - unless you just want to use it in demo mode!
Graham
|
|
|
|
|
An error occurred - System.IO.FileNotFoundException: \\TEKKAMAN3\wwwroot$\fcfossombrone.it\cp\test\pics\conpng.png at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at System.Drawing.Image.FromFile(String filename) at ASP.PNGUploader_aspx.UploadBtn_Click(Object sender, EventArgs e) in \\TEKKAMAN3\wwwroot$\fcfossombrone.it\cp\test\PNGUploader.aspx:line 52
|
|
|
|
|
Very good article to create thumbnail but to create images a bit larger it´s not so good, because the image lost the focus and quality.. If someone know a better solution, please post here..
Thanks
Gustavo R
|
|
|
|
|
I tried to resize image and in due time found GetThumbnailImage method of bitmap object. However, there was a problem - resulting image was distorted. Then I found this article and tried to replicate "png" solution. I'm not sure why but it didn't work. After that I started anew and came up with Bitmap constructor solution. There is no need in changing file format. This function saves existing image file in a specified folder (see below). I tested it, and it works.
Public Function ImageResize(ByVal ph_folder_ As String, _
ByVal ph_new_folder_ As String, _
ByVal v_new_folder_ As String, _
ByVal file_name_ As String, _
ByVal name_add_ As String, _
ByVal width As Integer) As System.Web.UI.WebControls.Image
Dim bm As New Bitmap(ph_folder_ + "\" + file_name_)
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)
Try
img.Save(ph_new_folder_ + "\" + Mid(file_name_, 1, InStrRev(file_name_, ".") - 1) + _
name_add_ + Mid(file_name_, InStrRev(file_name_, ".")))
Dim img_new As New System.Web.UI.WebControls.Image()
img_new.ImageUrl = v_new_folder_ + "/" + Mid(file_name_, 1, InStrRev(file_name_, ".") - 1) + _
name_add_ + Mid(file_name_, InStrRev(file_name_, "."))
Return img_new
Catch ex As Exception
_error = ex.Message
Return Nothing
End Try
End Function
|
|
|
|
|
this spraked my interest and as another viewer noted, the .net thumbnail thing doesn't work correctly with certian images. i have created a page that uploads the image and sizes it while keeping the proportions the same. here is the code, sub-optimal i'm sure.. but whatever...
<code><%@ Page Language="C#" %>
<%@ import namespace="System.IO" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
}
void UploadBut_Click(Object sender, EventArgs args)
{
String FilePath = Request.PhysicalApplicationPath + "\\test\\";
String ImageName = "camp_2005";
Int32 ImageWidth = 190;
Stream ImageStream = PhotoUpload.PostedFile.InputStream;
if ( PhotoUpload.PostedFile.FileName != "")
{
SaveUploadImageAsJpeg(ImageStream, FilePath, ImageName, ImageWidth);
SaveUploadImageAsGif(ImageStream, FilePath, ImageName, ImageWidth);
SaveUploadImageAsPng(ImageStream, FilePath, ImageName, ImageWidth);
}
}
void SaveUploadImageAsJpeg(Stream ImageStream, String FilePath, String ImageName, Int32 ImageWidth)
{
String Extension = ".jpg";
String MimeType = "image/jpeg";
long Quality = 95;
UploadImage(ImageStream, FilePath, ImageName, ImageWidth, Extension, MimeType, Quality);
}
void SaveUploadImageAsGif(Stream ImageStream, String FilePath, String ImageName, Int32 ImageWidth)
{
String Extension = ".gif";
String MimeType = "image/gif";
long Quality = 100;
UploadImage(ImageStream, FilePath, ImageName, ImageWidth, Extension, MimeType, Quality);
}
void SaveUploadImageAsPng(Stream ImageStream, String FilePath, String ImageName, Int32 ImageWidth)
{
String Extension = ".png";
String MimeType = "image/png";
long Quality = 0; // has no effect on png files
UploadImage(ImageStream, FilePath, ImageName, ImageWidth, Extension, MimeType, Quality);
}
void UploadImage(Stream ImageStream, String FilePath, String ImageName, Int32 ImageWidth, String Extension, String MimeType, long Quality )
{
try
{
System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(ImageStream);
Int32 old_height = UploadedImage.Height;
Int32 old_width = UploadedImage.Width;
Int32 new_width = ImageWidth;
Int32 new_height = (Int32) Math.Ceiling( (old_height * new_width)/old_width );
Bitmap smallerImg = new Bitmap(new_width, new_height);
Graphics g = Graphics.FromImage(smallerImg);
g.DrawImage(UploadedImage, 0, 0, new_width, new_height);
UploadedImage.Dispose();
if ( !Extension.Equals(".png") )
{
EncoderParameters myParams = new EncoderParameters(1);
myParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality );
smallerImg.Save( FilePath + ImageName + Extension, GetEncoderInfo(MimeType), myParams);
}
else
{
smallerImg.Save( FilePath + ImageName + Extension , ImageFormat.Png);
}
smallerImg.Dispose();
}
finally { }
}
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
{
return encoders[j];
}
}
return null;
}
// Validate the uploaded file is an image (.jpg, .jpeg, .gif)
void ValidatePicture(Object src, ServerValidateEventArgs args)
{
args.IsValid = false;
String fileName = PhotoUpload.PostedFile.FileName.ToLower();
if (fileName == "" || (fileName.IndexOf(".jpg") > -1) || (fileName.IndexOf(".jpeg") > -1) || (fileName.IndexOf(".gif") > -1)) args.IsValid = true;
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form id="MainForm" enctype="multipart/form-data" runat="server">
<table cellpadding="4" cellspacing="0" border="0">
<tr><td colspan="2" height="5"></td></tr>
<tr>
<td valign="top">Photo:</td>
<td><%
if ( new FileInfo(Application["PHYSICALPATH"] + "test/mytestImage.jpg").Exists )
{ %>
<div style="float:left; background-color:#666666; padding:1px;"><img src="../test/mytestImage.jpg" width="190"></div>
<div style="padding-top:8px; clear:both;"></div><br /> <%
}
if ( new FileInfo(Application["PHYSICALPATH"] + "test/mytestImage.gif").Exists )
{ %>
<div style="float:left; background-color:#666666; padding:1px;"><img src="../test/mytestImage.gif" width="190"></div>
<div style="padding-top:8px; clear:both;"></div><br /> <%
}
if ( new FileInfo(Application["PHYSICALPATH"] + "test/mytestImage.png").Exists )
{ %>
<div style="float:left; background-color:#666666; padding:1px;"><img src="../test/mytestImage.png" width="190"></div>
<div style="padding-top:8px; clear:both;"></div><br /> <%
} %>
<input id="PhotoUpload" runat="server" type="File" class="formcopy" />
<asp:customvalidator enableclientscript="false" cssclass="formcopy" display="dynamic" runat="server" text="* must be a .jpg, .jpeg, or .gif" controlToValidate="PhotoUpload" onservervalidate="ValidatePicture" />
</td>
</tr>
</table>
<br />
<table cellpadding="4" cellspacing="0" border="0">
<tr>
<td><asp:button ID="AddSaveButton" text="Upload" onclick="UploadBut_Click" cssclass="formcopy" runat="server" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html></code>
|
|
|
|
|
Update to the main method for maintianing quality of the image:
Bitmap smallerImg = new Bitmap(new_width, new_height);
Graphics g = Graphics.FromImage(smallerImg);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
foreach (PropertyItem pItem in UploadedImage.PropertyItems)
{
smallerImg.SetPropertyItem(pItem);
}
g.DrawImage(UploadedImage, 0, 0, new_width, new_height);
UploadedImage.Dispose();
if ( !Extension.Equals(".png") )
{
EncoderParameters myParams = new EncoderParameters(1);
myParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality );
smallerImg.Save( FilePath + ImageName + Extension, GetEncoderInfo(MimeType), myParams);
}
else
{
smallerImg.Save( FilePath + ImageName + Extension , ImageFormat.Png);
}
smallerImg.Dispose();
|
|
|
|
|
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..
|
|
|
|
|