Skip to main content

Convert Image Into Jpeg Using C sharp.

This example is useful to upload image of any format and Convert into JPEG or JPG Using C-Sharp.
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Drawing.Drawing2D;

public class _Library
{
public string w;
public string h;
public _Library()
{

}

/* for uploading Image */
public String UploadFile(FileUpload Fileobject,String Path)
{
String FileName = "";
String[] getformat;
String imgformat1 = "", imgformat2 = "";
if (Fileobject.HasFile)
{
FileName = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "_" + Fileobject.FileName.Replace(' ','-').ToLower();

Fileobject.SaveAs(@"" + Path + "" + FileName);
getformat =FileName.ToString().Split('.');
imgformat1 = getformat[0].ToString();
imgformat2 = getformat[1].ToString();

if (imgformat2.ToString().Equals("jpg"))
{
}
else
{
FileName = ConvertIntoJPEG(Path + "" + FileName);
}
}
return FileName;
}
public void UploadFile(FileUpload Fileobject, String NewFilePath, String NewFileName)
{
String FileName = "";
if (Fileobject.HasFile)
{
//FileName = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "_" + Fileobject.FileName.Replace(' ', '-').ToLower();
Fileobject.SaveAs(@"" + NewFilePath + "" + NewFileName);
FileName = ConvertIntoJPEG(NewFilePath + "" + NewFileName);

}
}
public bool ThumbnailCallback()
{
return false;
}
public void ImageResizeHigherSide(String Source_Desti,int image_higher_size)
{
String source = Source_Desti;
String desti = Source_Desti + "._tmpfile";
String Del_abspath = source;
Bitmap myBitmap = new Bitmap(source);
int higher_size = image_higher_size;
int h_w_n_h = 0;
int n_w = 0;
int n_h = 0;
int o_w = myBitmap.Width;
int o_h = myBitmap.Height;
if (o_w > higher_size || o_h > higher_size)
{
if (o_w > o_h)
{
n_w = higher_size;
n_h = o_h * higher_size / o_w;
}
else
{
n_h = higher_size;
n_w = o_w * higher_size / o_h;
}
try
{
//getting higher size of image
if (o_w > o_h) h_w_n_h = o_w;
else h_w_n_h = o_h;
if (image_higher_size > h_w_n_h)
{
n_h = o_h;
n_w = o_w;
}
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(this.ThumbnailCallback);
System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(n_w, n_h, myCallback, IntPtr.Zero);
////myBitmap.SetResolution(100, 100);
Graphics myresizer;
myresizer = Graphics.FromImage(myThumbnail);
myresizer.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
myresizer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
myresizer.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
myresizer.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

//////myresizer.DrawImage(myBitmap, new Rectangle(0, 0, n_w, n_h), 0, 0, n_w, n_h, GraphicsUnit.Pixel);
myresizer.DrawImage(myBitmap, 0, 0, n_w, n_h);
// myBitmap.Save(desti, ImageFormat.Jpeg);
myThumbnail.Save(desti, ImageFormat.Jpeg);
myresizer.Dispose();
myThumbnail.Dispose();
myBitmap.Dispose();
File.Delete(source);
File.Move(desti,desti.Replace("._tmpfile", ""));
File.Delete(desti);
}
catch (Exception ex){}
}
}


private String ConvertIntoJPEG(String Source_Desti)
{
String source = Source_Desti;
String ext = source.Substring(source.LastIndexOf('.'));
String desti = source;
desti = desti.Replace(ext, ".jpg");
desti = desti + "._tmpfile";
String nJpegFileName = "";
String Del_abspath = source;
Bitmap myBitmap = new Bitmap(source);

int o_w = myBitmap.Width;
int o_h = myBitmap.Height;
try
{
System.Drawing.Image.GetThumbnailImageAbort myCallback =
new System.Drawing.Image.GetThumbnailImageAbort(this.ThumbnailCallback);
System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(o_w, o_h, myCallback, IntPtr.Zero);


Graphics myresizer;
myresizer = Graphics.FromImage(myThumbnail);
myresizer.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
myresizer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
myresizer.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
myresizer.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

//////myresizer.DrawImage(myBitmap, new Rectangle(0, 0, n_w, n_h), 0, 0, n_w, n_h, GraphicsUnit.Pixel);
myresizer.DrawImage(myBitmap, 0, 0, o_w, o_h);

myThumbnail.Save(desti, ImageFormat.Jpeg);
myThumbnail.Dispose();
myBitmap.Dispose();
File.Delete(source);
File.Move(desti, desti.Replace("._tmpfile", ""));
File.Delete(desti);
}
catch (Exception ex)
{

}
desti = desti.Replace("._tmpfile", "");
nJpegFileName = desti.Substring(desti.LastIndexOf("\\") + 1);
return nJpegFileName;
}
/* For deleting Image from Folder*/
public void DeleteFile(String Del_abspath)
{
if (Del_abspath != null)
{
try
{
String delpath = Del_abspath.Replace('\\', '/');
File.Delete(delpath);
}
catch (Exception ex) { }

}
}
}

Comments

Popular posts from this blog

Flex Interview Questions

1) What is Flex? Flex is used to devleop Rich Internet Application (RIA) You can both desktop & web based applicaiton.It is markup language and object-oriented languages its many syntax match with flash action script. Flex Developers use typically five distinct phases to develoep Rich Internet Application. Design Configure Build Deploy Secure 2) How do i get Page URL and Query String ? We can use mx.core.Application.application.url (mx.core)package to work with current page URL. & query string. 3) Describe flex component file types ? There are following file types we can use in flex. extension .mxml - a component implemented as an MXML file. extension .as - a component implemented as an ActionScript class. extension .swc - a SWC file contains components in a packge. 4) Difference between target & currentTarget ? target : This property is set by the dispatchEvent() method. You cannot change this to a different object. currentTarget : This property is set by component instan...

Convert excel to CSV file using PHP, Linx Server

How to convert xls file into csv file using php or LAMP, Look at simple example to convert excel to csv file using php on linux server. <?php require_once 'Excel/reader.php'; $excel = new Spreadsheet_Excel_Reader(); $excel->setOutputEncoding('CP1251'); $excel->read('b.xls'); $x=1; $sep = ","; ob_start(); while($x<=$excel->sheets[0]['numRows']) { $y=1; $row=""; while($y<=$excel->sheets[0]['numCols']) { $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : ''; $row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\""; $y++; } echo $row."\n"; $x++; } $fp = fopen("data.csv",'w'); fwrite($fp,ob_get_contents()); fclose($fp); ob_end_cl...

How To Change Form Action at Runtime

How to change Form action at runtime using javascript. This the example which will change html form action at runtime. CODE: 1 :   <html> 2 :   <head> 3 :   <Script Language="javascript"> 4 :   function change_action(){ 5 :   var frm_obj=document.getElementById("frm"); 6 :   frm_obj.action="http://www.google.com"; 7 :   } 8 :   </Script> 9 :   </head> 10 :   <body> 11 :   <form id="frm" action="abc.php" method="post" onsubmit="return change_action()"> 12 :   UID &nbsp;&nbsp;<input type="text"><br> 13 :   PWD <input type="Password"><br><br> 14 :   <input type="submit" value="submit"> 15 :   </form> 16 :   </body> 17 :   </html>