Hi,
i have written a code for saving original image and saving resized image in database using asp.net c#.
string imgName = FileUpload1.FileName.ToString();
string imgPath = "~/upload/" + imgName;
string imgPath2 = "~/small upload/" + imgName;
filepath = Server.MapPath(imgPath);
filepath2 = Server.MapPath(imgPath2);
FileUpload1.SaveAs(Server.MapPath(imgPath));
ResizeImage(30,filepath,filepathsmall);
FileStream fs = new FileStream(filepath2, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
string strQuery = "INSERT INTO products(name, description, image,category,price,imagepath) VALUES(@NM, @EM, @PN,@CT,@PC,@IP)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@NM", name);
cmd.Parameters.AddWithValue("@EM", desc);
cmd.Parameters.AddWithValue("@PN", bytes);
cmd.Parameters.AddWithValue("@CT", category);
cmd.Parameters.AddWithValue("@PC", price);
cmd.Parameters.AddWithValue("@IP", imgPath2);
InsertData(cmd);
Functions
public static void ResizeImage(int size, string filePath, string saveFilePath)
{
double newHeight = 0;
double newWidth = 0;
double scale = 0;
Bitmap curImage = new Bitmap(filePath);
if (curImage.Height > curImage.Width)
{
scale = Convert.ToSingle(size) / curImage.Height;
}
else
{
scale = Convert.ToSingle(size) / curImage.Width;
}
if (scale < 0 || scale > 1) { scale = 1; }
newHeight = Math.Floor(Convert.ToSingle(curImage.Height) * scale);
newWidth = Math.Floor(Convert.ToSingle(curImage.Width) * scale);
Bitmap newImage = new Bitmap(curImage, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
Graphics imgDest = Graphics.FromImage(newImage);
imgDest.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
imgDest.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
imgDest.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
imgDest.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
EncoderParameters param = new EncoderParameters(1);
param.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
imgDest.DrawImage(curImage, 0, 0, newImage.Width, newImage.Height);
newImage.Save(saveFilePath, info[1], param);
curImage.Dispose();
newImage.Dispose();
imgDest.Dispose();
}
can you tell me in which format should we give size factor.when i compile i am getting error
A generic error occurred in GDI+.
can you help me solve this eeror or suggest me another code