asp.Net画像操作(Drawing.Imaging;Drawing)
70065 ワード
画像操作(Drawing.Imaging;Drawing)
1,Bitmap制御ピクチャ属性
グラフィック画像とその属性の画素データからなるGDI+ビットマップをカプセル化します.Bitmapは、画素データにより定義された画像を処理するためのオブジェクトである
使用方法:
Bitmap bitmap = new Bitmap();//作成
GetPixel()/ピクセルカラーの指定
Save()/指定した場所に保存
例:
2,Imageは、BitmapおよびMetafileのクラスに機能を提供する抽象ベースクラスである.
Imageタイプは以下のメンバーを公開します.
ツールバーの
名前
説明
Flags
イメージのピクセルデータの特性フラグを取得します.
FrameDimensionsList
このImageのフレームの次元数を表すGUIDの配列を取得する.
Height
このイメージの高さをピクセル単位で取得します.
HorizontalResolution
このイメージの水平解像度(「ピクセル/インチ」単位)を取得します.
Palette
このイメージに使用するパレットを取得または設定します.
PhysicalDimension
この画像の幅と高さを取得します.
PixelFormat
このイメージのピクセルフォーマットを取得します.
PropertyIdList
このImageに格納されている属性項目のIDを取得します.
PropertyItems
イメージに格納されているすべての属性アイテム(メタデータシート)を取得します.
RawFormat
このイメージのファイル形式を取得します.
Size
この画像の幅と高さをピクセル単位で取得します.
Tag
画像に関する追加データを提供するオブジェクトを取得または設定します.
VerticalResolution
このイメージの垂直解像度(「ピクセル/インチ」単位)を取得します.
Width
このイメージの幅をピクセル単位で取得します.
例:
など、一般的には他のクラスにimageタイプのパラメータ、または属性パラメータを提供します.
3,GraphicsパッケージのGDI+図面.
Clear(Color.Transparent)は、図面全体をクリアし、背景色の塗りつぶしを指定します.
DrawImage();指定した場所で、指定したサイズで描画します.
例:
全体の例:
図の一般的な操作
1,Bitmap制御ピクチャ属性
グラフィック画像とその属性の画素データからなるGDI+ビットマップをカプセル化します.Bitmapは、画素データにより定義された画像を処理するためのオブジェクトである
使用方法:
Bitmap bitmap = new Bitmap();//作成
GetPixel()/ピクセルカラーの指定
Save()/指定した場所に保存
例:
public Bitmap GetToMonochrome(Bitmap source, string fpath)
{
Bitmap bitmap = new Bitmap(source.Width, source.Height);
for (int i = 0; i < bitmap.Height; i++)
{
for (int j = 0; j < bitmap.Width; j++)
{
Color pixel = source.GetPixel(j, i);
Color c = System.Drawing.ColorTranslator.FromHtml("#69EE11");
int reda = pixel.A;
int redb = c.B;
int redg = c.G;
int redr = c.R;
bitmap.SetPixel(j, i, Color.FromArgb(reda, redr, redg, redb));
}
}
bitmap.Save(fpath.Substring(0, fpath.Length - 4) + "_BW.png", ImageFormat.Png);
bitmap.Dispose();
return bitmap;
}
2,Imageは、BitmapおよびMetafileのクラスに機能を提供する抽象ベースクラスである.
Imageタイプは以下のメンバーを公開します.
ツールバーの
名前
説明
Flags
イメージのピクセルデータの特性フラグを取得します.
FrameDimensionsList
このImageのフレームの次元数を表すGUIDの配列を取得する.
Height
このイメージの高さをピクセル単位で取得します.
HorizontalResolution
このイメージの水平解像度(「ピクセル/インチ」単位)を取得します.
Palette
このイメージに使用するパレットを取得または設定します.
PhysicalDimension
この画像の幅と高さを取得します.
PixelFormat
このイメージのピクセルフォーマットを取得します.
PropertyIdList
このImageに格納されている属性項目のIDを取得します.
PropertyItems
イメージに格納されているすべての属性アイテム(メタデータシート)を取得します.
RawFormat
このイメージのファイル形式を取得します.
Size
この画像の幅と高さをピクセル単位で取得します.
Tag
画像に関する追加データを提供するオブジェクトを取得または設定します.
VerticalResolution
このイメージの垂直解像度(「ピクセル/インチ」単位)を取得します.
Width
このイメージの幅をピクセル単位で取得します.
例:
Image original = Image.FromStream(“1.png”);
original.Height;
original.Width;
original.Tag;
など、一般的には他のクラスにimageタイプのパラメータ、または属性パラメータを提供します.
3,GraphicsパッケージのGDI+図面.
Clear(Color.Transparent)は、図面全体をクリアし、背景色の塗りつぶしを指定します.
DrawImage();指定した場所で、指定したサイズで描画します.
例:
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Transparent);
graphics.DrawImage(image, new Rectangle(0, 0, this.zipwidth, this.zipheight));
全体の例:
図の一般的な操作
#region
/// <summary>
///
/// </summary>
/// <param name="originalImagePath"> ( )</param>
/// <param name="thumbnailPath"> ( )</param>
/// <param name="width"> </param>
/// <param name="height"> </param>
/// <param name="mode"> </param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW": // ( )
break;
case "W": // ,
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": // ,
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut": // ( )
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
// bmp
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// ,
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
g.Clear(System.Drawing.Color.Transparent);
//
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
// jpg
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="path"> ( )</param>
/// <param name="waterpath"> ( )</param>
/// <param name="location"> ( )</param>
public static string ImageWatermark(string path, string waterpath, string location)
{
string kz_name = Path.GetExtension(path);
if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
{
DateTime time = DateTime.Now;
string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
Image img = Bitmap.FromFile(path);
Image waterimg = Image.FromFile(waterpath);
Graphics g = Graphics.FromImage(img);
ArrayList loca = GetLocation(location, img, waterimg);
g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
waterimg.Dispose();
g.Dispose();
string newpath = Path.GetDirectoryName(path) + filename + kz_name;
img.Save(newpath);
img.Dispose();
File.Copy(newpath, path, true);
if (File.Exists(newpath))
{
File.Delete(newpath);
}
}
return path;
}
/// <summary>
///
/// </summary>
/// <param name="location"> </param>
/// <param name="img"> </param>
/// <param name="waterimg"> </param>
private static ArrayList GetLocation(string location, Image img, Image waterimg)
{
ArrayList loca = new ArrayList();
int x = 0;
int y = 0;
if (location == "LT")
{
x = 10;
y = 10;
}
else if (location == "T")
{
x = img.Width / 2 - waterimg.Width / 2;
y = img.Height - waterimg.Height;
}
else if (location == "RT")
{
x = img.Width - waterimg.Width;
y = 10;
}
else if (location == "LC")
{
x = 10;
y = img.Height / 2 - waterimg.Height / 2;
}
else if (location == "C")
{
x = img.Width / 2 - waterimg.Width / 2;
y = img.Height / 2 - waterimg.Height / 2;
}
else if (location == "RC")
{
x = img.Width - waterimg.Width;
y = img.Height / 2 - waterimg.Height / 2;
}
else if (location == "LB")
{
x = 10;
y = img.Height - waterimg.Height;
}
else if (location == "B")
{
x = img.Width / 2 - waterimg.Width / 2;
y = img.Height - waterimg.Height;
}
else
{
x = img.Width - waterimg.Width;
y = img.Height - waterimg.Height;
}
loca.Add(x);
loca.Add(y);
return loca;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="path"> ( )</param>
/// <param name="size"> </param>
/// <param name="letter"> </param>
/// <param name="color"> </param>
/// <param name="location"> </param>
public static string LetterWatermark(string path, int size, string letter, Color color, string location)
{
#region
string kz_name = Path.GetExtension(path);
if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
{
DateTime time = DateTime.Now;
string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
Image img = Bitmap.FromFile(path);
Graphics gs = Graphics.FromImage(img);
ArrayList loca = GetLocation(location, img, size, letter.Length);
Font font = new Font(" ", size);
Brush br = new SolidBrush(color);
gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString()));
gs.Dispose();
string newpath = Path.GetDirectoryName(path) + filename + kz_name;
img.Save(newpath);
img.Dispose();
File.Copy(newpath, path, true);
if (File.Exists(newpath))
{
File.Delete(newpath);
}
}
return path;
#endregion
}
/// <summary>
///
/// </summary>
/// <param name="location"> </param>
/// <param name="img"> </param>
/// <param name="width"> ( , )</param>
/// <param name="height"> ( , )</param>
private static ArrayList GetLocation(string location, Image img, int width, int height)
{
#region
ArrayList loca = new ArrayList(); //
float x = 10;
float y = 10;
if (location == "LT")
{
loca.Add(x);
loca.Add(y);
}
else if (location == "T")
{
x = img.Width / 2 - (width * height) / 2;
loca.Add(x);
loca.Add(y);
}
else if (location == "RT")
{
x = img.Width - width * height;
}
else if (location == "LC")
{
y = img.Height / 2;
}
else if (location == "C")
{
x = img.Width / 2 - (width * height) / 2;
y = img.Height / 2;
}
else if (location == "RC")
{
x = img.Width - height;
y = img.Height / 2;
}
else if (location == "LB")
{
y = img.Height - width - 5;
}
else if (location == "B")
{
x = img.Width / 2 - (width * height) / 2;
y = img.Height - width - 5;
}
else
{
x = img.Width - width * height;
y = img.Height - width - 5;
}
loca.Add(x);
loca.Add(y);
return loca;
#endregion
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="mybm"> </param>
/// <param name="width"> </param>
/// <param name="height"> </param>
/// <param name="val"> </param>
public Bitmap LDPic(Bitmap mybm, int width, int height, int val)
{
Bitmap bm = new Bitmap(width, height);//
int x, y, resultR, resultG, resultB;//x、y ,
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//
resultR = pixel.R + val;// [0, 255]
resultG = pixel.G + val;// [0, 255]
resultB = pixel.B + val;// [0, 255]
bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//
}
}
return bm;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="mybm"> </param>
/// <param name="width"> </param>
/// <param name="height"> </param>
public Bitmap RePic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);//
int x, y, resultR, resultG, resultB;
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//
resultR = 255 - pixel.R;//
resultG = 255 - pixel.G;//
resultB = 255 - pixel.B;//
bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//
}
}
return bm;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="oldBitmap"> </param>
/// <param name="Width"> </param>
/// <param name="Height"> </param>
public Bitmap FD(Bitmap oldBitmap, int Width, int Height)
{
Bitmap newBitmap = new Bitmap(Width, Height);
Color color1, color2;
for (int x = 0; x < Width - 1; x++)
{
for (int y = 0; y < Height - 1; y++)
{
int r = 0, g = 0, b = 0;
color1 = oldBitmap.GetPixel(x, y);
color2 = oldBitmap.GetPixel(x + 1, y + 1);
r = Math.Abs(color1.R - color2.R + 128);
g = Math.Abs(color1.G - color2.G + 128);
b = Math.Abs(color1.B - color2.B + 128);
if (r > 255) r = 255;
if (r < 0) r = 0;
if (g > 255) g = 255;
if (g < 0) g = 0;
if (b > 255) b = 255;
if (b < 0) b = 0;
newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
return newBitmap;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="bmp"> </param>
/// <param name="newW"> </param>
/// <param name="newH"> </param>
public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap bap = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(bap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(bap, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bap.Width, bap.Height), GraphicsUnit.Pixel);
g.Dispose();
return bap;
}
catch
{
return null;
}
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="mybm"> </param>
/// <param name="width"> </param>
/// <param name="height"> </param>
public Bitmap FilPic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);//
int x, y;
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//
bm.SetPixel(x, y, Color.FromArgb(0, pixel.G, pixel.B));//
}
}
return bm;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="mybm"> </param>
/// <param name="width"> </param>
/// <param name="height"> </param>
public Bitmap RevPicLR(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);
int x, y, z; //x,y ,z x
Color pixel;
for (y = height - 1; y >= 0; y--)
{
for (x = width - 1, z = 0; x >= 0; x--)
{
pixel = mybm.GetPixel(x, y);//
bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//
}
}
return bm;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="mybm"> </param>
/// <param name="width"> </param>
/// <param name="height"> </param>
public Bitmap RevPicUD(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);
int x, y, z;
Color pixel;
for (x = 0; x < width; x++)
{
for (y = height - 1, z = 0; y >= 0; y--)
{
pixel = mybm.GetPixel(x, y);//
bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//
}
}
return bm;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="oldfile"> </param>
/// <param name="newfile"> </param>
public bool Compress(string oldfile, string newfile)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = new Size(100, 125);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x]; // JPEG
break;
}
img.Dispose();
if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
outBmp.Dispose();
return true;
}
catch
{
return false;
}
}
#endregion
#region
public Color Gray(Color c)
{
int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
return Color.FromArgb(rgb, rgb, rgb);
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="mybt"> </param>
/// <param name="width"> </param>
/// <param name="height"> </param>
public Bitmap BWPic(Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap(width, height);
int x, y, result; //x,y ,result
Color pixel;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
pixel = mybm.GetPixel(x, y);//
result = (pixel.R + pixel.G + pixel.B) / 3;//
bm.SetPixel(x, y, Color.FromArgb(result, result, result));
}
}
return bm;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="pPath"> </param>
/// <param name="pSavePath"> </param>
public void GetFrames(string pPath, string pSavedPath)
{
Image gif = Image.FromFile(pPath);
FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
int count = gif.GetFrameCount(fd); // (gif , )
for (int i = 0; i < count; i++) // Jpeg
{
gif.SelectActiveFrame(fd, i);
gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
}
}
#endregion