asp.Net透かしを追加するコード(テスト済み)

7320 ワード

ウォーターマークの機能コードは以下の通りです
 
  
///
/// , ,
///

public class picmark
{
#region "member fields"
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
#endregion
public picmark()
{
}
#region "propertys"
///
///
///

public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
///
/// ( )
///

public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
///
///
///

public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
//
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
///
/// ,
///

public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
///
///
///

public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
#endregion
#region "methods"
///
///
///

public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
//
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
//
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
//
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
//
string[] allowImageType = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
#endregion
}

フロントコードは以下の通りです
 
  




<br/> <br/> <br/><form id="form1" runat="server"> <br/><div> <br/><fileupload id="FileUpload1" runat="server"/> <br/><br/> <br/><button id="Button1" runat="server" onclick="Button1_Click" text="Button"/> <br/></div> <br/></form> <br/> <br/> <br/> </code></pre> <br/>cs <br/> <pre><code> <br/>protected void Button1_Click(object sender, EventArgs e) <br/>{ <br/>string extension = Path.GetExtension(this.FileUpload1.FileName).ToUpper(); <br/>string fileName = Guid.NewGuid().ToString(); <br/>string savePath = Server.MapPath("../upfile/" + fileName+ extension); <br/>if (!Directory.Exists(Path.GetDirectoryName(savePath))) <br/>{ <br/>Directory.CreateDirectory(Path.GetDirectoryName(savePath)); <br/>} <br/>this.FileUpload1.SaveAs(savePath); <br/>// <br/>picmark wm = new picmark(); <br/>wm.DrawedImagePath = Server.MapPath("/upfile/" + "backlogo.gif") ; <br/>wm.ModifyImagePath = savePath; <br/>wm.RightSpace = 145; <br/>wm.BottoamSpace =17; <br/>wm.LucencyPercent = 50; <br/>wm.OutPath = Server.MapPath("/upfile/" + fileName.Replace("-","").ToUpper() + extension); <br/>wm.DrawImage(); <br/>//fileName = "_New_" + fileName; <br/>//string sPath = Server.MapPath("../upfile/" + fileName + extension); <br/>//this.FileUpload1.SaveAs(sPath); <br/>// , <br/>if (File.Exists(savePath)) <br/>{ <br/>File.Delete(savePath); <br/>//File.Delete(wm.OutPath); <br/>} <br/> </code></pre> <div class="clearfix"> <span id="art_bot" class="jbTestPos"/> </div> </div> </div> </div>