using System . Text;
using System . IO;
using System.Drawing;
using System.Windows.Forms;
using System. Drawing. Imaging;
using System. Drawing. Drawing2D;
public class ImageProcess
{
public enum Dimensions
{
Width ,
Height
}
public enum AnchorPosition
{
Top ,
Center ,
Bottom ,
Left ,
Right
}
public byte [ ] GetBytesFromFile ( String pathFileName )
{
FileInfo info = new FileInfo ( pathFileName );
byte [ ] buffer = File. ReadAllBytes ( pathFileName );
return buffer;
}
public Byte [ ] GetBytesByPictuerBox ( PictureBox pictuerBox )
{
MemoryStream ms = new MemoryStream ( );
pictuerBox . Image . Save ( ms , pictuerBox . Image . RawFormat );
Byte[] retBytes=ms . GetBuffer ( );
return retBytes;
}
public Image GetImageByBytes ( Byte [ ] imageBytes )
{
try
{
Image retImage;
MemoryStream ms = new MemoryStream ( imageBytes );
retImage = Image. FromStream ( ms );
return retImage;
}
catch
{
return null;
}
}
public Image GetFixedSize ( Image imgPhoto , int Width , int Height )
{
int sourceWidth = imgPhoto. Width;
int sourceHeight = imgPhoto. Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ( ( float ) Width / ( float ) sourceWidth );
nPercentH = ( ( float ) Height / ( float ) sourceHeight );
if ( nPercentH < nPercentW )
{
nPercent = nPercentH;
destX = System. Convert. ToInt16 ( ( Width -
( sourceWidth * nPercent ) ) / 2 );
}
else
{
nPercent = nPercentW;
destY = System. Convert. ToInt16 ( ( Height -
( sourceHeight * nPercent ) ) / 2 );
}
int destWidth = ( int ) ( sourceWidth * nPercent );
int destHeight = ( int ) ( sourceHeight * nPercent );
Bitmap bmPhoto = new Bitmap ( Width , Height ,
PixelFormat. Format24bppRgb );
bmPhoto. SetResolution ( imgPhoto. HorizontalResolution ,
imgPhoto. VerticalResolution );
Graphics grPhoto = Graphics. FromImage ( bmPhoto );
grPhoto. Clear ( Color. Red );
grPhoto. InterpolationMode =
InterpolationMode. HighQualityBicubic;
grPhoto. DrawImage ( imgPhoto ,
new Rectangle ( destX , destY , destWidth , destHeight ) ,
new Rectangle ( sourceX , sourceY , sourceWidth , sourceHeight ) ,
GraphicsUnit. Pixel );
grPhoto. Dispose ( );
return bmPhoto;
}
public Image GetImageScaleByPercent ( Image imgPhoto , int Percent )
{
float nPercent = ( ( float ) Percent / 100 );
int sourceWidth = imgPhoto. Width;
int sourceHeight = imgPhoto. Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = ( int ) ( sourceWidth * nPercent );
int destHeight = ( int ) ( sourceHeight * nPercent );
Bitmap bmPhoto = new Bitmap ( destWidth , destHeight ,
PixelFormat. Format24bppRgb );
bmPhoto. SetResolution ( imgPhoto. HorizontalResolution ,
imgPhoto. VerticalResolution );
Graphics grPhoto = Graphics. FromImage ( bmPhoto );
grPhoto. InterpolationMode = InterpolationMode. HighQualityBicubic;
grPhoto. DrawImage ( imgPhoto ,
new Rectangle ( destX , destY , destWidth , destHeight ) ,
new Rectangle ( sourceX , sourceY , sourceWidth , sourceHeight ) ,
GraphicsUnit. Pixel );
grPhoto. Dispose ( );
return bmPhoto;
}
public Image GetCropedImageByAnchorPoint ( Image imgPhoto , int Width ,
int Height , AnchorPosition Anchor )
{
int sourceWidth = imgPhoto. Width;
int sourceHeight = imgPhoto. Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ( ( float ) Width / ( float ) sourceWidth );
nPercentH = ( ( float ) Height / ( float ) sourceHeight );
if ( nPercentH < nPercentW )
{
nPercent = nPercentW;
switch ( Anchor )
{
case AnchorPosition. Top:
destY = 0;
break;
case AnchorPosition. Bottom:
destY = ( int )
( Height - ( sourceHeight * nPercent ) );
break;
default:
destY = ( int )
( ( Height - ( sourceHeight * nPercent ) ) / 2 );
break;
}
}
else
{
nPercent = nPercentH;
switch ( Anchor )
{
case AnchorPosition. Left:
destX = 0;
break;
case AnchorPosition. Right:
destX = ( int )
( Width - ( sourceWidth * nPercent ) );
break;
default:
destX = ( int )
( ( Width - ( sourceWidth * nPercent ) ) / 2 );
break;
}
}
int destWidth = ( int ) ( sourceWidth * nPercent );
int destHeight = ( int ) ( sourceHeight * nPercent );
Bitmap bmPhoto = new Bitmap ( Width ,
Height , PixelFormat. Format24bppRgb );
bmPhoto. SetResolution ( imgPhoto. HorizontalResolution ,
imgPhoto. VerticalResolution );
Graphics grPhoto = Graphics. FromImage ( bmPhoto );
grPhoto. InterpolationMode =
InterpolationMode. HighQualityBicubic;
grPhoto. DrawImage ( imgPhoto ,
new Rectangle ( destX , destY , destWidth , destHeight ) ,
new Rectangle ( sourceX , sourceY , sourceWidth , sourceHeight ) ,
GraphicsUnit. Pixel );
grPhoto. Dispose ( );
return bmPhoto;
}
public Byte[] GetBytesByImage(Image image)
{
PictureBox pictuerBox = new PictureBox();
pictuerBox.Image = image;
MemoryStream ms = new MemoryStream();
pictuerBox.Image.Save(ms, pictuerBox.Image.RawFormat);
Byte[] retBytes = ms.GetBuffer();
return retBytes;
}
}
No comments :
Post a Comment