Created
July 12, 2009 00:27
-
-
Save rafaelss/145450 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Helper | |
{ | |
public static class Convert | |
{ | |
public static int ToInt(object value) | |
{ | |
try | |
{ | |
return System.Convert.ToInt32(value); | |
} | |
catch | |
{ | |
return 0; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using DrawingImage = System.Drawing.Image; | |
namespace Helper | |
{ | |
public class Image | |
{ | |
public static DrawingImage Resize(DrawingImage image, Size size) | |
{ | |
float percentWidth = ((float)size.Width / (float)image.Width); | |
float percentHeight = ((float)size.Height / (float)image.Height); | |
float percent = percentWidth; | |
if (percentHeight < percentWidth) | |
{ | |
percent = percentHeight; | |
} | |
int width = (int)(image.Width * percent); | |
int height = (int)(image.Height * percent); | |
DrawingImage thumb = new Bitmap(width, height, image.PixelFormat); | |
using (Graphics graphic = Graphics.FromImage(thumb)) | |
{ | |
Rectangle rectangle = new Rectangle(0, 0, width, height); | |
graphic.CompositingQuality = CompositingQuality.HighQuality; | |
graphic.SmoothingMode = SmoothingMode.HighQuality; | |
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
graphic.DrawImage(image, rectangle); | |
} | |
return thumb; | |
} | |
public static void SaveJpeg(string path, DrawingImage image, long quality) | |
{ | |
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); | |
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); | |
if (jpegCodec != null) | |
{ | |
EncoderParameters encoderParams = new EncoderParameters(1); | |
encoderParams.Param[0] = qualityParam; | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
image.Save(ms, jpegCodec, encoderParams); | |
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) | |
{ | |
byte[] buffer = ms.GetBuffer(); | |
fs.Write(buffer, 0, buffer.Length); | |
} | |
} | |
} | |
} | |
private static ImageCodecInfo GetEncoderInfo(string mimeType) | |
{ | |
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); | |
for (int i = 0; i < codecs.Length; i++) | |
{ | |
if (codecs[i].MimeType == mimeType) | |
{ | |
return codecs[i]; | |
} | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace Helper | |
{ | |
public static class String | |
{ | |
public static string Cleanup(string filename) | |
{ | |
Dictionary<char, char> replacement = new Dictionary<char, char>(); | |
replacement['Š'] = 'S'; | |
replacement['š'] = 's'; | |
replacement['Đ'] = 'D'; | |
replacement['đ'] = 'd'; | |
replacement['Ž'] = 'Z'; | |
replacement['ž'] = 'z'; | |
replacement['Č'] = 'C'; | |
replacement['č'] = 'c'; | |
replacement['Ć'] = 'C'; | |
replacement['ć'] = 'c'; | |
replacement['À'] = 'A'; | |
replacement['Á'] = 'A'; | |
replacement['Â'] = 'A'; | |
replacement['Ã'] = 'A'; | |
replacement['Ä'] = 'A'; | |
replacement['Å'] = 'A'; | |
replacement['Æ'] = 'A'; | |
replacement['Ç'] = 'C'; | |
replacement['È'] = 'E'; | |
replacement['É'] = 'E'; | |
replacement['Ê'] = 'E'; | |
replacement['Ë'] = 'E'; | |
replacement['Ì'] = 'I'; | |
replacement['Í'] = 'I'; | |
replacement['Î'] = 'I'; | |
replacement['Ï'] = 'I'; | |
replacement['Ñ'] = 'N'; | |
replacement['Ò'] = 'O'; | |
replacement['Ó'] = 'O'; | |
replacement['Ô'] = 'O'; | |
replacement['Õ'] = 'O'; | |
replacement['Ö'] = 'O'; | |
replacement['Ø'] = 'O'; | |
replacement['Ù'] = 'U'; | |
replacement['Ú'] = 'U'; | |
replacement['Û'] = 'U'; | |
replacement['Ü'] = 'U'; | |
replacement['Ý'] = 'Y'; | |
replacement['Þ'] = 'B'; | |
replacement['ß'] = 'S'; | |
replacement['à'] = 'a'; | |
replacement['á'] = 'a'; | |
replacement['â'] = 'a'; | |
replacement['ã'] = 'a'; | |
replacement['ä'] = 'a'; | |
replacement['å'] = 'a'; | |
replacement['æ'] = 'a'; | |
replacement['ç'] = 'c'; | |
replacement['è'] = 'e'; | |
replacement['é'] = 'e'; | |
replacement['ê'] = 'e'; | |
replacement['ë'] = 'e'; | |
replacement['ì'] = 'i'; | |
replacement['í'] = 'i'; | |
replacement['î'] = 'i'; | |
replacement['ï'] = 'i'; | |
replacement['ð'] = 'o'; | |
replacement['ñ'] = 'n'; | |
replacement['ò'] = 'o'; | |
replacement['ó'] = 'o'; | |
replacement['ô'] = 'o'; | |
replacement['õ'] = 'o'; | |
replacement['ö'] = 'o'; | |
replacement['ø'] = 'o'; | |
replacement['ù'] = 'u'; | |
replacement['ú'] = 'u'; | |
replacement['û'] = 'u'; | |
replacement['ý'] = 'y'; | |
replacement['ý'] = 'y'; | |
replacement['þ'] = 'b'; | |
replacement['ÿ'] = 'y'; | |
replacement['Ŕ'] = 'R'; | |
replacement['ŕ'] = 'r'; | |
replacement[' '] = '-'; | |
char[] chars = filename.ToCharArray(); | |
foreach (char chr in chars) | |
{ | |
if (replacement.ContainsKey(chr)) | |
{ | |
filename = filename.Replace(chr, replacement[chr]); | |
} | |
} | |
return filename; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment