using System;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.IO;
namespace SupportBLL
{
class Resize
{
static string _tempLocation = "E:/TestFolder/";
public void ResizeSamples()
{
CheckAndSendToResize("D:/Myimage.jpg", "Myimage.jpg");
}
private Boolean CheckAndSendToResize(string SourcePath, string fileName)
{
float ActualHeight = 0, ActualWidth = 0;
float ResizedHeight = 0, ResizedWidth = 0;
Single intPercentage;
bool IsResize = false;
try
{
if (File.Exists(SourcePath))
{
LooggingOnScreen("File Exist :");
Bitmap bmSourceFile = new Bitmap(SourcePath, true);
ActualHeight = bmSourceFile.PhysicalDimension.Height;
ActualWidth = bmSourceFile.PhysicalDimension.Width;
if (ActualHeight > 1000 || ActualWidth > 1000)
{
if (ActualHeight > ActualWidth)
{
IsResize = true;
ResizedHeight = 1000;
intPercentage = (1000 * 100) / ActualHeight;
ResizedWidth = (ActualWidth * intPercentage) / 100;
}
else if (ActualWidth > ActualHeight)
{
IsResize = true;
ResizedWidth = 1000;
intPercentage = (1000 * 100) / ActualWidth;
ResizedHeight = (ActualHeight * intPercentage) / 100;
}
else if(ActualWidth == ActualHeight)
{
IsResize = true;
ResizedWidth = 1000;
ResizedHeight = 1000;
}
if (IsResize)
{
try
{
if (ResizeandSaveAtTempLoacation(bmSourceFile, _tempLocation + fileName, ResizedWidth, ResizedHeight)) {
//Delete From Original Location
DeleteFile(SourcePath);
LooggingOnScreen("Deleted Original :");
MoveFile(_tempLocation + fileName, SourcePath);
LooggingOnScreen("Moved Resized :");
}
} catch (Exception ex)
{
LooggingOnScreen(ex.Message);
}
}
} else
{
LooggingOnScreen("No need of Resize");
}
} else
{
}
} catch (Exception ex)
{
LooggingOnScreen(ex.Message);
}
return true;
}
private Boolean ResizeandSaveAtTempLoacation(Bitmap bmSourceFile, string FileDestinationPath, float ResizedWidth, float ResizedHeight)
{
LooggingOnScreen("Doing Resize :");
Boolean returnValue = false;
try
{
Bitmap bmDustnation = new Bitmap(Convert.ToInt32(ResizedWidth), Convert.ToInt32(ResizedHeight));
Graphics objGraphics = Graphics.FromImage(bmDustnation);
objGraphics.DrawImage(bmSourceFile, 0, 0, ResizedWidth + 1, ResizedHeight + 1);
bmDustnation.Save(FileDestinationPath, System.Drawing.Imaging.ImageFormat.Jpeg);
bmSourceFile.Dispose();
bmDustnation.Dispose();
returnValue = true;
} catch (Exception Ex)
{
LooggingOnScreen(Ex.Message);
returnValue = false;
}
return returnValue;
}
private void DeleteFile(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
private void MoveFile(string SourceLoaction, string DestinationLocation)
{
if (File.Exists(SourceLoaction))
{
File.Move(SourceLoaction, DestinationLocation);
}
}
private void LooggingOnScreen(string text)
{
if (Environment.UserInteractive)
{
Console.WriteLine(text);
}
}
}
}The code is used to resize files and save back on same location.
