1.如何通过c#读写(JPG格式)的源码摘要信息
如何通过c#读写(JPG格式)的摘要信息
using System;
using System.Collections.Generic;
using System.Text;
namespace SnhjCms.Common
{
/// <summary>
/// 处理类
/// </summary>
public static class PictureUtil
{
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="imgPath">原路径</param>
/// <param name="breviaryPath">缩略图路径</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成方式</param>
public static void MakeBreviaryPhoto(string imgPath, string breviaryPath, int width, int height, string mode)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(imgPath);
int x = 0, y = 0;
int w = image.Width;
int h = image.Height;
switch (mode)
{
case "HW"://指定高度和宽度缩放(会变形)
break;
case "W"://指定宽度缩放,高度按比例。源码磁性自瞄源码
height = h * width / w;
break;
case "H"://指定高度缩放,源码iapp源码资源盒宽度按比例。源码源码之家爱给
width = w * height / h;
break;
case "Cut"://指定高度和宽度裁剪。源码荆州联通直播源码
if ((double)w / (double)h > (double)width / (double)height)
{
w = h * width / height;
x = (image.Width - w) / 2;
}
else
{
h = w * height / width;
y = (image.Height - h) / 2;
}
break;
default:
break;
}
//创建一个bmp
System.Drawing.Image bmpImg = new System.Drawing.Bitmap(width,源码白鸟源码论坛分享 height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpImg);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原的指定部分
g.DrawImage(image, new System.Drawing.Rectangle(0,0,width,height), new System.Drawing.Rectangle(x,y,w,h), System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bmpImg.Save(breviaryPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
throw e;
}
finally
{
image.Dispose();
bmpImg.Dispose();
g.Dispose();
}
}
/// <summary>
/// 加水印
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="watermarkFilename">水印文件名</param>
/// <param name="watermarkStatus">水印位置:0=不使用 1=左上 2=中上 3=右上 4=左中 ... 9=右下</param>
/// <param name="quality">是否是高质量 取值范围0--</param>
/// <param name="watermarkTransparency">水印透明度 取值范围1-- (为不透明)</param>
public static void AddImageSignPic(string Path, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
System.Drawing.Image watermark = new System.Drawing.Bitmap(watermarkFilename);
if (watermark.Height >= img.Height || watermark.Width >= img.Width)
{
return;
}
System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
colorMap.OldColor = System.Drawing.Color.FromArgb(, 0, , 0);
colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0);
System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
float transparency = 0.5F;
if (watermarkTransparency >= 1 && watermarkTransparency <= )
{
transparency = (watermarkTransparency / .0F);
}
float[][] colorMatrixElements = {
new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] { 0.0f, 0.0f, 0.0f, transparency, 0.0f},
new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
int xpos = 0;
int ypos = 0;
switch (watermarkStatus)
{
case 1:
xpos = (int)(img.Width * (float).);
ypos = (int)(img.Height * (float).);
break;
case 2:
xpos = (int)((img.Width * (float).) - (watermark.Width / 2));
ypos = (int)(img.Height * (float).);
break;
case 3:
xpos = (int)((img.Width * (float).) - (watermark.Width));
ypos = (int)(img.Height * (float).);
break;
case 4:
xpos = (int)(img.Width * (float).);
ypos = (int)((img.Height * (float).) - (watermark.Height / 2));
break;
case 5:
xpos = (int)((img.Width * (float).) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).) - (watermark.Height / 2));
break;
case 6:
xpos = (int)((img.Width * (float).) - (watermark.Width));
ypos = (int)((img.Height * (float).) - (watermark.Height / 2));
break;
case 7:
xpos = (int)(img.Width * (float).);
ypos = (int)((img.Height * (float).) - watermark.Height);
break;
case 8:
xpos = (int)((img.Width * (float).) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).) - watermark.Height);
break;
case 9:
xpos = (int)((img.Width * (float).) - (watermark.Width));
ypos = (int)((img.Height * (float).) - watermark.Height);
break;
}
g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes);
System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo ici = null;
foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
{
ici = codec;
}
}
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > )
{
quality = ;
}
qualityParam[0] = quality;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;
if (ici != null)
{
img.Save(filename, ici, encoderParams);
}
else
{
img.Save(filename);
}
g.Dispose();
img.Dispose();
watermark.Dispose();
imageAttributes.Dispose();
}
/// <summary>
/// 在上生成水印
/// </summary>
/// <param name="Path">原服务器路径</param>
/// <param name="Path_syp">生成的带水印的路径</param>
/// <param name="Path_sypf">水印路径</param>
public static void AddWaterPic(string Path, string Path_syp, string Path_sypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
image.Save(Path_syp);
image.Dispose();
}
}
}