asp.Netでサムネイルを生成し、著作権インスタンスコードを追加

3744 ワード

 
  
// image
Drawing.Image image,newimage;
//
protected string imagePath;
//
protected string imageType;
//
protected string imageName;
// , Image
// GetThumbnailImage , true; false
System.Drawing.Image.GetThumbnailImageAbort callb = null;

private void sm_Click(object sender, System.EventArgs e)
{
string mPath;

if("" != File1.PostedFile.FileName) //File1
{
imagePath = File1.PostedFile.FileName;
//
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
//
imageName = imagePath.Substring(imagePath.LastIndexOf("\\")+1);
// JPG GIF , ,
if("jpg" != imageType && "gif" != imageType)
{
Response.Write(" alert(' ! jpg gif !');");
return;
}
else
{
try
{
//
mPath=Server.MapPath("UploadFiles");
//
File1.PostedFile.SaveAs(mPath+"\\"+imageName);

// , imageSource
//imageSource.ImageUrl = "UploadFiles/"+imageName;

//
image=System.Drawing.Image.FromFile(mPath+"\\"+imageName);
//
newimage=image.GetThumbnailImage(200,200,callb,new System.IntPtr());
//
newimage.Save(Server.MapPath("UploadFiles")+"\\small"+imageName);
// image
image.Dispose();
// newimage
newimage.Dispose();
//

AddTextToImg ("UploadFiles/"+"small"+imageName,"Pic Info"); //
Image1.ImageUrl = "UploadFiles/"+"small"+imageName;

Script.Alert(" !");
}
catch
{
Script.Alert(" !");
}

} // end else
}

// ,
// AddTextToImg (physicPath,"Pic Info");
private void AddTextToImg(string fileName,string text)
{
//string sss = MapPath(fileName);

if ( !File.Exists ( fileName)) {
throw new FileNotFoundException("The file don't exist!");
}

// ,

System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);//MapPath(fileName));
Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
Graphics g = Graphics.FromImage(bitmap);

float fontSize = 22.0f; //
float textWidth = text.Length*fontSize; //
// ,
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length*(fontSize+18);
float rectHeight = fontSize+18;
//
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
Font font = new Font(" ",fontSize);//
Brush whiteBrush = new SolidBrush(Color.White);
Brush blackBrush = new SolidBrush(Color.Black);
g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString(text,font,whiteBrush,textArea);
MemoryStream ms = new MemoryStream();
// Jpg
bitmap.Save(ms,ImageFormat.Jpeg);

// , ,
/**//* Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite( ms.ToArray() );
*/
FileStream fs=new FileStream(fileName, FileMode.OpenOrCreate);//.CreateNew);
fs.Write(ms.ToArray(),0,ms.ToArray().Length);
fs.Close();

Image1.ImageUrl = fileName; // Image
g.Dispose();
bitmap.Dispose();
image.Dispose();
}