using (var image = Image.FromStream(sourcePath))
{
// can give static width (e.g. 1024) of image as we want
var newWidth = (int)(scaleFactor * image.Width);
//You can give static height (e.g. 1024) of image as we want
var newHeight = (int)(scaleFactor * image.Height);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
}
}
#endregion
Namespace I used is
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Image = System.Drawing.Image;
It's passing A generic error code in GDI+ . Please short out the problem.
Banwari
Member
28 Points
67 Posts
Image Resizing Problem
Dec 30, 2012 05:52 AM|LINK
Hello
I am trying to resize the uplaoding image .Here's my code
if (true)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
targetPath = Server.MapPath("CarPic/" + filename);
Stream strm = FileUpload1.PostedFile.InputStream;
var targetFile = targetPath;
string path1 = "~/CarPic/" + filename;
GenerateThumbnails(0.5, strm, targetFile);
user.Photo = path1;
}
else
{
//Label1.Text = "File size not valid";
}
#region ImageCompress
private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
{
using (var image = Image.FromStream(sourcePath))
{
// can give static width (e.g. 1024) of image as we want
var newWidth = (int)(scaleFactor * image.Width);
//You can give static height (e.g. 1024) of image as we want
var newHeight = (int)(scaleFactor * image.Height);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
}
}
#endregion
Namespace I used is
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Image = System.Drawing.Image;
It's passing A generic error code in GDI+ . Please short out the problem.
urenjoy
Star
12367 Points
1862 Posts
Re: Image Resizing Problem
Dec 30, 2012 06:23 AM|LINK
Make sure targetPath exists and the app has sufficient permission to access the path. Make sure the image should not be used in another processing
http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream
http://forums.asp.net/t/624305.aspx/1
http://forums.asp.net/t/1766006.aspx/1
Banwari
Member
28 Points
67 Posts
Re: Image Resizing Problem
Dec 30, 2012 06:50 AM|LINK
In my one project this code is working fine but in my current project passing error. I am using the same code same namespace . Why ?
oned_gk
All-Star
31762 Points
6490 Posts
Re: Image Resizing Problem
Dec 30, 2012 09:31 AM|LINK
oned_gk
All-Star
31762 Points
6490 Posts
Re: Image Resizing Problem
Dec 30, 2012 09:34 AM|LINK