using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Net;
using System.IO;
namespace ImgHelp
{
public class ImgHelp
{
public static byte[]
cutImage(string originalImagePath,
string thumbnailPath, int width, int height)
{
Image image = Image.FromFile(originalImagePath);
int num = width;
int num2 = height;
int x = 0;
int y = 0;
int num5 =
image.Width;
int num6 =
image.Height;
if (num >=
num2)
{
num2 = (num6 * num) / num5;
if (num2 <
height)
{
num2 = height;
num = (num5 * num2) / num6;
}
}
else
{
num = (num5 * num2) / num6;
if (num <
width)
{
num = width;
num2 = (num6 * num) / num5;
}
}
Image image2 = new Bitmap(num, num2);
Graphics graphics =
Graphics.FromImage(image2);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(image, new
Rectangle(0, 0, num, num2),
new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
num6 = height;
num5 = width;
y = (num2 - num6) / 2;
x = (num - num5) / 2;
Image image3 = new Bitmap(width, height);
Graphics graphics2 =
Graphics.FromImage(image3);
graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics2.SmoothingMode = SmoothingMode.AntiAlias;
graphics2.Clear(Color.Transparent);
graphics2.DrawImage(image2, new
Rectangle(0, 0, width, height),
new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
image3 = KiContrast(KiLighten(KiSharpen((Bitmap)image3, float.Parse("0.5")), 5), 10);
int num7 = 100;
ImageCodecInfo[] imageEncoders
= ImageCodecInfo.GetImageEncoders();
ImageCodecInfo encoder =
null;
foreach (ImageCodecInfo info2 in imageEncoders)
{
if (info2.MimeType == "image/jpeg")
{
encoder = info2;
}
}
EncoderParameters encoderParams
= new EncoderParameters();
encoderParams.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
(long)num7);
Bitmap img = new Bitmap(image3);
//if (img.Width >=
300)
//{
//img = AddImageSignPic(img, "",
HttpContext.Current.Server.MapPath("shuiyin.png"), 9, 100,
10);
// }
try
{
img.Save(thumbnailPath, encoder, encoderParams);
FileStream stream2 =
new FileStream(thumbnailPath, FileMode.OpenOrCreate);
//stream2.getb
byte[] buffer = new byte[stream2.Length];
stream2.Read(buffer, 0, buffer.Length);
stream2.Seek(0, SeekOrigin.Begin);
stream2.Close();
image.Dispose();
return buffer;
}
catch (Exception)
{
return null;
}
finally
{
image.Dispose();
image2.Dispose();
}
}
public static unsafe
Bitmap KiContrast(Bitmap b, int degree)
{
if (b == null)
{
return null;
}
if (degree <
-100)
{
degree = -100;
}
if (degree >
100)
{
degree = 100;
}
try
{
double num = 0.0;
double num2 = (100.0 + degree) /
100.0;
num2 *= num2;
int width = b.Width;
int
height = b.Height;
BitmapData bitmapdata =
b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
byte* numPtr = (byte*)bitmapdata.Scan0;
int num5 = bitmapdata.Stride -
(width * 3);
for (int i = 0; i < height;
i++)
{
for (int j = 0; j < width;
j++)
{
for (int k = 0; k < 3;
k++)
{
num
= ((((((double)numPtr[k]) / 255.0)
- 0.5) * num2) + 0.5) * 255.0;
if (num <
0.0)
{
num = 0.0;
}
if (num >
255.0)
{
num = 255.0;
}
numPtr[k] = (byte)num;
}
numPtr += 3;
}
numPtr += num5;
}
b.UnlockBits(bitmapdata);
return b;
}
catch
{
return null;
}
}
public static unsafe
Bitmap KiLighten(Bitmap b, int degree)
{
if (b == null)
{
return null;
}
if (degree <
-255)
{
degree = -255;
}
if (degree >
0xff)
{
degree = 0xff;
}
try
{
int width = b.Width;
int height =
b.Height;
int num3 =
0;
BitmapData bitmapdata =
b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
byte* numPtr = (byte*)bitmapdata.Scan0;
int num4 = bitmapdata.Stride -
(width * 3);
for (int i = 0; i < height;
i++)
{
for (int j = 0; j < width;
j++)
{
for (int k = 0; k < 3;
k++)
{
num3
= numPtr[k] + degree;
if (degree <
0)
{
numPtr[k] = (byte)Math.Max(0, num3);
}
if (degree >
0)
{
numPtr[k] = (byte)Math.Min(0xff, num3);
}
}
numPtr += 3;
}
numPtr += num4;
}
b.UnlockBits(bitmapdata);
return b;
}
catch
{
return null;
}
}
public static unsafe
Bitmap KiSharpen(Bitmap b, float val)
{
if (b == null)
{
return null;
}
int width = b.Width;
int height =
b.Height;
try
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bitmapdata =
b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
BitmapData data2 =
bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
byte* numPtr = (byte*)bitmapdata.Scan0.ToPointer();
byte* numPtr2 = (byte*)data2.Scan0.ToPointer();
int stride =
bitmapdata.Stride;
for (int i = 0; i < height;
i++)
{
for (int j = 0; j < width;
j++)
{
if ((((j == 0) || (j == (width -
1))) || (i == 0)) || (i == (height - 1)))
{
numPtr2[0]
= numPtr[0];
numPtr2[1] = numPtr[1];
numPtr2[2] = numPtr[2];
}
else
{
byte* numPtr3 = (numPtr - stride)
- 3;
int num6 =
numPtr3[2];
int num15 =
numPtr3[1];
int num24 =
numPtr3[0];
numPtr3 = numPtr - stride;
int
num7 = numPtr3[2];
int num16 =
numPtr3[1];
int num25 =
numPtr3[0];
numPtr3 = (numPtr - stride) + 3;
int num8 =
numPtr3[2];
int num17 =
numPtr3[1];
int num26 =
numPtr3[0];
numPtr3 = numPtr - 3;
int num9 =
numPtr3[2];
int num18 =
numPtr3[1];
int num27 =
numPtr3[0];
numPtr3 = numPtr + 3;
int num10 =
numPtr3[2];
int num19 =
numPtr3[1];
int num28 =
numPtr3[0];
numPtr3 = (numPtr + stride) - 3;
int num11 =
numPtr3[2];
int num20 =
numPtr3[1];
int num29 =
numPtr3[0];
numPtr3
= numPtr + stride;
int num12 =
numPtr3[2];
int num21 =
numPtr3[1];
int num30 =
numPtr3[0];
numPtr3 = (numPtr + stride) + 3;
int num13 =
numPtr3[2];
int num22 =
numPtr3[1];
int num31 =
numPtr3[0];
numPtr3 = numPtr;
int num14 =
numPtr3[2];
int
num23 = numPtr3[1];
int num32 =
numPtr3[0];
float num33 = num14 -
(((float)(((((((num6 + num7) +
num8) + num9) + num10) + num11) + num12) + num13)) /
8f);
float
num34 = num23 - (((float)(((((((num15 + num16) + num17) + num18)
+ num19) + num20) + num21) + num22)) / 8f);
float num35 = num32 -
(((float)(((((((num24 + num25) +
num26) + num27) + num28) + num29) + num30) + num31)) /
8f);
num33 = num14 + (num33 * val);
num34 = num23 + (num34 * val);
num35 = num32 + (num35 * val);
if (num33 >
0f)
{
num33 = Math.Min(255f,
num33);
}
else
{
num33 = Math.Max(0f,
num33);
}
if (num34 >
0f)
{
num34 = Math.Min(255f,
num34);
}
else
{
num34 = Math.Max(0f,
num34);
}
if (num35 >
0f)
{
num35 = Math.Min(255f,
num35);
}
else
{
num35 = Math.Max(0f,
num35);
}
numPtr2[0]
= (byte)num35;
numPtr2[1] = (byte)num34;
numPtr2[2] = (byte)num33;
}
numPtr += 3;
numPtr2 += 3;
}
numPtr += bitmapdata.Stride - (width * 3);
numPtr2 += bitmapdata.Stride - (width * 3);
}
b.UnlockBits(bitmapdata);
bitmap.UnlockBits(data2);
return bitmap;
}
catch
{
return null;
}
}
}
}
|