|
<summary>
Picture processing
</summary>
public static Class Imagehelper
{
public static string Cropimage (byte[] content, int x, int y, int cropwidth, int cropheight)
{
using (MemoryStream stream = new MemoryStream (content))
{
Return Cropimage (stream, x, Y, Cropwidth, cropheight);
}
}
public static string Cropimage (Stream content, int x, int y, int cropwidth, int cropheight)
{
using (Bitmap Sourcebitmap = new Bitmap (content))
{
Zoom the selected picture
Bitmap Bitsource = new Bitmap (Sourcebitmap, Sourcebitmap.width, sourcebitmap.height);
Rectangle croprect = new Rectangle (x, Y, Cropwidth, cropheight);
using (Bitmap Newbitmap = new Bitmap (Cropwidth, Cropheight))
{
Newbitmap.setresolution (Sourcebitmap.horizontalresolution, sourcebitmap.verticalresolution);
using (Graphics g = graphics.fromimage (Newbitmap))
{
G.interpolationmode = Interpolationmode.highqualitybicubic;
G.smoothingmode = smoothingmode.highquality;
G.pixeloffsetmode = pixeloffsetmode.highquality;
g.compositingquality = compositingquality.highquality;
G.drawimage (Bitsource, New Rectangle (0, 0, Newbitmap.width, newbitmap.height), Croprect, GraphicsUnit.Pixel);
Return Getbitmapbytes (NEWBITMAP);
}
}
}
}
public static string Getbitmapbytes (Bitmap source)
{
ImageCodecInfo codec = Imagecodecinfo.getimageencoders () [4];
EncoderParameters parameters = new EncoderParameters (1);
Parameters. Param[0] = new Encoderparameter (encoder.quality, 100L);
using (MemoryStream tmpstream = new MemoryStream ())
{
Source. Save (Tmpstream, codec, parameters);
byte[] data = new Byte[tmpstream.length];
Tmpstream.seek (0, Seekorigin.begin);
Tmpstream.read (data, 0, (int) tmpstream.length);
string result = convert.tobase64string (data);
return result;
}
}
<summary>
Picture Conversion Base64
</summary>
<param name= "Urlorpath" > Image network address or local path </param>
<returns></returns>
public static string ImageToBase64 (String urlorpath)
{
Try
{
if (Urlorpath.contains ("http"))
{
Uri url = new Uri (Urlorpath);
using (var webClient = new WebClient ())
{
var imgdata = webclient.downloaddata (URL);
using (var ms = new MemoryStream (imgdata))
{
byte[] data = new Byte[ms. Length];
Ms. Seek (0, seekorigin.begin);
Ms. Read (data, 0, Convert.ToInt32 (MS). Length));
String netimage = convert.tobase64string (data);
return netimage;
}
}
}
Else
{
FileStream FileStream = new FileStream (Urlorpath, FileMode.Open);
Stream stream = FileStream as stream;
byte[] data = new Byte[stream. Length];
Stream. Seek (0, seekorigin.begin);
Stream. Read (data, 0, Convert.ToInt32 (stream). Length));
String netimage = convert.tobase64string (data);
return netimage;
}
}
catch (Exception)
{
return null;
}
}
<summary>
Scale pictures proportionally
</summary>
<param name= "Content" ></param>
<param name= "Resizewidth" ></param>
<returns></returns>
public static string Resizeimage (Stream content, int resizewidth, int resizeheight)
{
using (Bitmap Sourcebitmap = new Bitmap (content))
{
int width = sourcebitmap.width,
Height = sourcebitmap.height;
if (Height > resizeheight | | | width > resizewidth)
{
if ((Width * resizeheight) > (height * resizewidth))
{
Resizeheight = (resizewidth * height)/width;
}
Else
{
Resizewidth = (Width * resizeheight)/height;
}
}
Else
{
Resizewidth = width;
Resizeheight = height;
}
Zoom the selected picture
Bitmap Bitsource = new Bitmap (Sourcebitmap, Resizewidth, resizeheight);
Bitsource.setresolution (Sourcebitmap.horizontalresolution, sourcebitmap.verticalresolution);
using (MemoryStream stream = new MemoryStream ())
{
Bitsource.save (stream, imageformat.jpeg);
byte[] data = new Byte[stream. Length];
Stream. Seek (0, seekorigin.begin);
Stream. Read (data, 0, Convert.ToInt32 (stream). Length));
String newimage = convert.tobase64string (data);
return newimage;
}
}
}
} |