Showing posts with label Image Resize. Show all posts
Showing posts with label Image Resize. Show all posts

Thursday, January 22, 2009

Image resize using GetThumbnailImage Function

Image resize using .GetThumbnailImage Function
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO.Compression;
using System.Drawing.Drawing2D;


public partial class ImageResize : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Image thumbnailImage;
Bitmap originalImage;

originalImage = new Bitmap("E:\\Retreat.jpg");
int imgHeight = originalImage.Height;
int imgWidht = originalImage.Width;
string format = Convert.ToString(originalImage.RawFormat);
ImageFormat imageFormat;

switch (format)
{
case ("gif"):
imageFormat = ImageFormat.Gif;
break;
default:
imageFormat = ImageFormat.Jpeg;
format = "jpg";
break;
}

if (imgWidht == 0)
{ imgWidht = 100; }

if (imgHeight == 0)
{ imgHeight = imgWidht; }

string ImageSavePath = "E:\\jj1234.jpg";
thumbnailImage = originalImage.GetThumbnailImage(imgWidht, imgHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailImageAbortDelegate), IntPtr.Zero);
thumbnailImage.Save(ImageSavePath, imageFormat);
originalImage.Dispose();
}
protected static bool ThumbnailImageAbortDelegate() { return false; }
}


ASP.NET Resize images proportionately

ASP.NET Resize images proportionately
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO.Compression;
using System.Drawing.Drawing2D;


public partial class ImageResize : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ImageSavePath = "E:\\jj1.jpg";
string BasePath = Server.MapPath(".");
Bitmap Img, ImgTemp;

ImgTemp = new Bitmap("E:\\Retreat.jpg");
int imgHeight = ImgTemp.Height;
int imgWidht = ImgTemp.Width;

Img = new Bitmap(ImgTemp,imgWidht*40/100, imgHeight*30/100);
Img.Save(ImageSavePath, ImageFormat.Jpeg);

ImgTemp.Dispose();
Img.Dispose();

//Graphics Graph;
//Graph = Graphics.FromImage(Img);
//Graph.DrawImage(Img, 100, 100);

}
}

Thursday, June 26, 2008

Previewing image while opening ASP.NET

Previewing image while opening

We can use the following code to preview an image while opening it using ASP.NET, C#, Java script.

<SCRIPT>
var lastfieldname = ''
function preview(i)
{
var file = i.value
if (file.length<=0) return;
var ipreview = document.all('ipreview')
ipreview.src ='file://' + file
}
img id="ipreview" border="1" height="120" src="images/spacer.gif" /><br />
adjust image:
<input id="fotofp" runat="server" name="file1" onchange="preview(this)" onfocus="preview(this)" size="25" type="file" />

Monday, June 09, 2008

Restricting File Upload size using ASP.NET C#

We can use the following code to restrict the File upload size using Asp.Net, C#.


We can set the maximum upload limit for the file, using maxRequestLength in web.config of your ASP.NET web application.

Here is the sample code for this...


protected void btnUpload_Click(object sender, System.EventArgs e)
{
string fileName = FileSelector.PostedFile.FileName;
int fN = fileName.LastIndexOf("\\");
string newFileName = fileName.Substring(fN).ToLower();

if (newFileName.EndsWith(".m4v") || newFileName.EndsWith(".mp3") || newFileName.EndsWith(".mp4") || newFileName.EndsWith(".mov"))
{
if (this.FileSelector.PostedFile.ContentLength < 210000)
{
FileSelector.PostedFile.SaveAs(
Server.MapPath("../audioFiles/") + newFileName);

lblWarn.Text = "File uploaded";
lblWarn.ForeColor = System.Drawing.Color.Green;
lblWarn.Visible = true;
}
else
{
lblWarn.Text = "File cannot exceed 350MB";
lblWarn.Visible = true;
lblWarn.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblWarn.Text = "Only extensions .m4v, .mp4, mp3, or .mov";
lblWarn.Visible = true;
lblWarn.ForeColor = System.Drawing.Color.Red;
}
}

Also default upload limit is 4MB in asp.net. You can change this value in ur web.config file.

<httpRuntime maxRequestLength="210000"/>

Include this in your webconfig file in the system.web section

You can also use FileUpload1.PostedFile.ContentLength to calculate the length of the file after it is uploaded to server.

Image size, type Upload Validation

protected void Button1_Click(object sender, EventArgs e)
{
string fileFullname = this.File1.PostedFile.FileName;


if (File1.PostedFile.ContentType.ToUpper().IndexOf("IMAGE") > -1)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;

if (Width > 200 || Height > 200 || File1.PostedFile.ContentLength > 1024 * 1024 * 200)
{
Response.Write("<script language='javascript'>alert('your image don not confirm the with and height.!');</script>");
}
else
{
if (type == "jpg" || type == "gif" || type == "bmp" || type == "JPG" || type == "GIF")
{

string sPath = Server.MapPath("images/") + dataName + fileName;

string imgPath = "pic/" + dataName + fileName;

this.File1.PostedFile.SaveAs(sPath);

Response.Write("<script language='javascript'>alert('suceed!!!');</script>");


this.Image1.ImageUrl = imgPath;


this.Button1.Enabled = false;
this.Button1.Text = "suceed!";
this.Button1.Enabled = true;

}
else
{
Response.Write("<script language='javascript'>alert('your file type wrong!');</script>");
}
}
}
else
{

Response.Write("<script language='javascript'>alert('please choice the image to upload!');</script>");
}
}

Validations::
We can also use RegularExpressionValidator to validate the file (extensions) which you are uploading. The RE will look like this ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG)$

and now you have to set RequiredFieldValidator for fileUpload. It stops you from proceeding further if you do not upload a file.

Friday, June 06, 2008

Image Resize using ASP.NET C# and displaying as Thumbnails dynamically

The following code helps us to Resize images and display them as thumbnails dynamically using ASP.NET C#.

protected void Page_Load(object sender, EventArgs e)
{
string BasePath = Server.MapPath(".");
Bitmap Img, ImgTemp;

ImgTemp = new Bitmap(BasePath + "\\images\\Balaji.jpg");
Img = new Bitmap(ImgTemp, 200, 300);

Graphics Graph;
Graph = Graphics.FromImage(Img);

Img.Save(Response.OutputStream, ImageFormat.Jpeg);
Graph.DrawImage(Img, 0, 0);

ImgTemp.Dispose();

Img.Dispose();
}

This function helps us to set the height and width of the new image Bitmap(ImgTemp, 200, 300);