i wanna make image resizer that resize image automatiically.but my code only make rectangle and it will make only sizes 200x200,300x300,100x100 in thin manner. I also want to make Image in square like 100x80,80x200,200x120,300x128 like this . but my coed
make only Rectangle image but i want to make square image also .can anyone know about my problem how i resolve .
Below is my Code:
imageResize.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
namespace Testweb
{
public class imageResize
{
public static byte[] ResizeFromByteArray(int MaxSideSize,Byte[] byteArrayIn, string fileName)
{
byte[] byteArray = null; // really make this an error gif
MemoryStream ms = new MemoryStream(byteArrayIn);
byteArray = imageResize.ResizeFromStream(MaxSideSize,ms,fileName);
return byteArray;
}
public static byte[] ResizeFromStream(int MaxSideSize,Stream Buffer, string fileName)
{
byte[] byteArray = null; // really make this an error gif
try
{
Bitmap bitMap = new Bitmap(Buffer);
int intOldWidth = bitMap.Width;
int intOldHeight = bitMap.Height;
int intNewWidth;
int intNewHeight;
int intMaxSide;
/// <summary>
/// Saves the resized image to specified file name and path as JPEG
/// and also returns the bytearray for any other use you may need it for
/// </summary>
/// <param name="MaxSideSize"></param>
/// <param name="Buffer"></param>
/// <param name="fileName">No Extension</param>
/// <param name="filePath">Examples: "images/dir1/dir2" or "images" or "images/dir1"</param>
public static byte[] SaveFromStream(int MaxSideSize, Stream Buffer, string fileName, string filePath)
{
byte[] byteArray = null; // really make this an error gif
try
{
Bitmap bitMap = new Bitmap(Buffer);
int intOldWidth = bitMap.Width;
int intOldHeight = bitMap.Height;
int intNewWidth;
int intNewHeight;
int intMaxSide;
MemoryStream ms = new MemoryStream();
oThumbNail.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));
oGraphic.Dispose();
oImg.Dispose();
ms.Close();
ms.Dispose();
}
catch (Exception)
{
int newSize = MaxSideSize - 20;
Bitmap bitMap = new Bitmap(newSize, newSize);
Graphics g = Graphics.FromImage(bitMap);
g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, newSize, newSize));
Font font = new Font("Courier", 8);
SolidBrush solidBrush = new SolidBrush(Color.Red);
g.DrawString("Failed To Save File or Failed File", font, solidBrush, 10, 5);
g.DrawString(fileName, font, solidBrush, 10, 50);
MemoryStream ms = new MemoryStream();
bitMap.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));
Thanks for the reply ,i got the link that you send me for image resizer and it work well. but i have still one problem if i am use this code for Animated gif image then it will not work and only one frame is resize and animation is destroyed. how can i
use this code for animated image and resize the Animated gif image ans also i want to resize image in bulk .can you send me anything that helpful for me
My problem is not resolve for resize a animated image .can you have any code for doing this.These links is not helpful for me that you send me for animated image resize .
Hi, Can you told me how i resize the Animated image in C# without lost any frames of animated image if you have any code fro doing this , then share with me or suggest me how i acheive this
Can you told me how i resize the animated image without lost its frams and animation.Previously you send me some links but this was not helpful for me .so , can you told em that how i resize the animated image .
ervipingupta
Member
17 Points
75 Posts
Image Resizer in C#
Jun 24, 2009 12:42 PM|LINK
Hello Friends ,
i wanna make image resizer that resize image automatiically.but my code only make rectangle and it will make only sizes 200x200,300x300,100x100 in thin manner. I also want to make Image in square like 100x80,80x200,200x120,300x128 like this . but my coed make only Rectangle image but i want to make square image also .can anyone know about my problem how i resolve .
Below is my Code:
imageResize.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
namespace Testweb
{
public class imageResize
{
public static byte[] ResizeFromByteArray(int MaxSideSize,Byte[] byteArrayIn, string fileName)
{
byte[] byteArray = null; // really make this an error gif
MemoryStream ms = new MemoryStream(byteArrayIn);
byteArray = imageResize.ResizeFromStream(MaxSideSize,ms,fileName);
return byteArray;
}
public static byte[] ResizeFromStream(int MaxSideSize,Stream Buffer, string fileName)
{
byte[] byteArray = null; // really make this an error gif
try
{
Bitmap bitMap = new Bitmap(Buffer);
int intOldWidth = bitMap.Width;
int intOldHeight = bitMap.Height;
int intNewWidth;
int intNewHeight;
int intMaxSide;
if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
Size ThumbNailSize = new Size(intNewWidth, intNewHeight);
System.Drawing.Image oImg = System.Drawing.Image.FromStream(Buffer);
System.Drawing.Image oThumbNail = new Bitmap (ThumbNailSize.Width, ThumbNailSize.Height);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle
(0, 0, ThumbNailSize.Width, ThumbNailSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
MemoryStream ms = new MemoryStream();
oThumbNail.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));
oGraphic.Dispose();
oImg.Dispose();
ms.Close();
ms.Dispose();
}
catch (Exception)
{
int newSize = MaxSideSize-20;
Bitmap bitMap = new Bitmap(newSize, newSize);
Graphics g = Graphics.FromImage(bitMap);
g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0,0, newSize, newSize));
Font font = new Font("Courier", 8);
SolidBrush solidBrush = new SolidBrush(Color.Red);
g.DrawString("Failed File", font, solidBrush, 10, 5);
g.DrawString(fileName, font, solidBrush, 10, 50);
MemoryStream ms = new MemoryStream();
bitMap.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));
ms.Close();
ms.Dispose();
bitMap.Dispose();
solidBrush.Dispose();
g.Dispose();
font.Dispose();
}
return byteArray;
}
/// <summary>
/// Saves the resized image to specified file name and path as JPEG
/// and also returns the bytearray for any other use you may need it for
/// </summary>
/// <param name="MaxSideSize"></param>
/// <param name="Buffer"></param>
/// <param name="fileName">No Extension</param>
/// <param name="filePath">Examples: "images/dir1/dir2" or "images" or "images/dir1"</param>
public static byte[] SaveFromStream(int MaxSideSize, Stream Buffer, string fileName, string filePath)
{
byte[] byteArray = null; // really make this an error gif
try
{
Bitmap bitMap = new Bitmap(Buffer);
int intOldWidth = bitMap.Width;
int intOldHeight = bitMap.Height;
int intNewWidth;
int intNewHeight;
int intMaxSide;
if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
Size ThumbNailSize = new Size(intNewWidth, intNewHeight);
System.Drawing.Image oImg = System.Drawing.Image.FromStream(Buffer);
System.Drawing.Image oThumbNail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle
(0, 0, ThumbNailSize.Width, ThumbNailSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
//Save File
string newFileName = string.Format(System.Web.HttpContext.Current.Server.MapPath("~/{0}*{1}.jpg"), filePath, fileName);
oThumbNail.Save(newFileName, ImageFormat.Jpeg);
MemoryStream ms = new MemoryStream();
oThumbNail.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));
oGraphic.Dispose();
oImg.Dispose();
ms.Close();
ms.Dispose();
}
catch (Exception)
{
int newSize = MaxSideSize - 20;
Bitmap bitMap = new Bitmap(newSize, newSize);
Graphics g = Graphics.FromImage(bitMap);
g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, newSize, newSize));
Font font = new Font("Courier", 8);
SolidBrush solidBrush = new SolidBrush(Color.Red);
g.DrawString("Failed To Save File or Failed File", font, solidBrush, 10, 5);
g.DrawString(fileName, font, solidBrush, 10, 50);
MemoryStream ms = new MemoryStream();
bitMap.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));
ms.Close();
ms.Dispose();
bitMap.Dispose();
solidBrush.Dispose();
g.Dispose();
font.Dispose();
}
return byteArray;
}
}
}
Default.aspx
byte[] imgNew;
imgNew = imageResize.ResizeFromStream(110, FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName);
Response.BinaryWrite(imgNew);
byte[] imgNewSave;
imgNewSave = imageResize.SaveFromStream(110, FileUpload1.PostedFile.InputStream, "newPicFromResize", "images");
Please help , it is urgent .
Thanx in Advance
Vipin
chintanpshah
All-Star
19058 Points
3273 Posts
Re: Image Resizer in C#
Jun 24, 2009 01:49 PM|LINK
Use one of these:
http://www.codeproject.com/KB/web-image/pnguploader.aspx
http://www.4guysfromrolla.com/articles/012203-1.aspx
http://nathanaeljones.com/products/asp-net-image-resizer/
http://forums.asp.net/p/1433920/3229333.aspx
My Software Website
ervipingupta
Member
17 Points
75 Posts
Re: Image Resizer in C#
Jun 25, 2009 12:24 PM|LINK
Hi Friend,
http://www.codeproject.com/KB/web-image/pnguploader.aspx
Thanks for the reply ,i got the link that you send me for image resizer and it work well. but i have still one problem if i am use this code for Animated gif image then it will not work and only one frame is resize and animation is destroyed. how can i use this code for animated image and resize the Animated gif image ans also i want to resize image in bulk .can you send me anything that helpful for me
Thanks in Advance again
Vipin
chintanpshah
All-Star
19058 Points
3273 Posts
Re: Image Resizer in C#
Jun 25, 2009 01:05 PM|LINK
Refer to:
http://stackoverflow.com/questions/539034/how-to-copy-an-animated-gif-image-using-c
http://www.codeproject.com/KB/GDI-plus/NGif.aspx
Third Party:
http://www.atalasoft.com/kb/article.aspx?id=10086
http://www.aspjpeg.com/manual_09.html
My Software Website
ervipingupta
Member
17 Points
75 Posts
Re: Image Resizer in C#
Jun 26, 2009 05:16 AM|LINK
Hi Friend,
My problem is not resolve for resize a animated image .can you have any code for doing this.These links is not helpful for me that you send me for animated image resize .
please Help me
Thanks in advance
Vipin
ervipingupta
Member
17 Points
75 Posts
Re: Image Resizer in C#
Jul 01, 2009 06:55 AM|LINK
Hi, Can you told me how i resize the Animated image in C# without lost any frames of animated image if you have any code fro doing this , then share with me or suggest me how i acheive this
Regards
vipin
ervipingupta
Member
17 Points
75 Posts
Re: Image Resizer in C#
Jul 06, 2009 05:37 AM|LINK
Hi friend,
Can you told me how i resize the animated image without lost its frams and animation.Previously you send me some links but this was not helpful for me .so , can you told em that how i resize the animated image .
Thanks in Advance
Vipin