A C # class for website pictures plus stamp

zhaozj2021-02-12  166

Many websites are copyrighted by their own picture, and they need to be uploaded with copyright symbols, this C # Class In order to deal with such problems. This is an expansion that is written yesterday, adding a text C # process, in addition to adding text on the picture, you can specify the seal in the form of the picture.

The Stamper function is used to add a seal to the image, and Save can save the picture to the specified file or in IO stream.

Using system; using system.drawing; using system.drawing.drawing2d; using system.drawing.image;

Namespace mysite {///

/// stamperimage gives the image plus seal. /// Public Class StamperImage: IDisposable

{Public enum position: int {Topleft = 1, TOPRIGHT = 2, BottomleaseFt = 3, Bottomright = 4, Center = 5}

int _margin = 3; private Position _stampPos = Position.TopLeft; private string _originImageFile = "", _stampImageFile = ""; private Bitmap _bmOriginImage, _bmStampImage; private string _stampText = ""; private bool _textStampMode = false; private Font _textFont = new Font ("Arial, Song", 10); Private Color_TextColor = Color.White; private bool_dispose = false

public StamperImage () {_dispose = true;} public StamperImage (string sOriginImageFile, string sStampImageFile) {_originImageFile = sOriginImageFile; _stampImageFile = sStampImageFile; try {_bmOriginImage = new Bitmap (_originImageFile); _bmStampImage = new Bitmap (_stampImageFile);} catch (Exception e) {throw e;}} public StamperImage (Bitmap sOrginImage, Bitmap sStampImage) {_bmOriginImage = sOrginImage; _bmStampImage = sStampImage;} public StamperImage (string sOriginImageFile, string sStampText, Font sStampTextFont, Color sStampTextColor) {_originImageFile = sOriginImageFile; try {_bmOriginImage = new Bitmap (_originImageFile);} catch (Exception e) {throw e;} _textStampMode = true; _stampText = sStampText; _textFont = sStampTextFont; _textColor = sStampTextColor;} public StamperImage (string sOriginImageFile, string sStampText, Font sStampTextFont) {_originImageFile = Soriginimagefile; try {_bmoriginim age = new Bitmap (_originImageFile);} catch (Exception e) {throw e;} _textStampMode = true; _stampText = sStampText; _textFont = sStampTextFont;} public StamperImage (string sOriginImageFile, string sStampText, Color sStampTextColor) {_originImageFile = sOriginImageFile; try {_BMORIGINIMAGE = New bitmap (_ORIGINIMAGEFILE);} catch (exception e) {throw e;} _textstampmode = true; _stamptext = sstamptext; _TextColor = sstamptextColor;}

private void Dispose (bool disposing) {if (! _ dispose) {if (disposing) {if (! _bmOriginImage = null) _bmOriginImage.Dispose (); if (_bmStampImage = null!) _bmStampImage.Dispose (); _dispose = true;} }

Public void dispose () {dispose (true); gc.suppressFinalize (this);} ///

/// give a picture plus seal /// public void stamper () {int _ioImagewith, _ioImageHeight ; Point _stampPoint; try {Graphics _graSettingImage = Graphics.FromImage (_bmOriginImage); _iOImageWith = _bmOriginImage.Width; _iOImageHeight = _bmOriginImage.Height; if (_textStampMode) {int _iFontHeight = _textFont.Height; int _iFontWith = ((int) _textFont.SizeInPoints * _stampText.Length); switch (_stampPos) {case Position.TopLeft: _stampPoint = new Point (_margin, _margin); break; case Position.TopRight: _stampPoint = new Point (_iOImageWith - _margin - _iFontWith, _margin); break; case Position.BottomLeft: _stampPoint = new Point (_margin, _iOImageHeight - _iFontHeight - _margin); break; case Position.BottomRight: _stampPoint = new Point (_iOImageWith - _margin - _iFontWith, _iOImageHeight - _iFontHeight - _margin); break; case Position.Center: _stamppoint = ne w Point (_iOImageWith / 2 - _iFontWith / 2, _iOImageHeight / 2 - _iFontHeight / 2); break; default: _stampPoint = new Point (_margin, _margin); break;} _graSettingImage.DrawString (_stampText, _textFont, new SolidBrush (_textColor) , _stampPoint);} else {int _iSImageWith = _bmStampImage.Width; int _iSImageHeight = _bmStampImage.Height; switch (_stampPos) {case Position.TopLeft: _stampPoint = new Point (_margin, _margin); break; case Position.TopRight: _stampPoint = New point (_ioImagewith - _Margin); _Margin; Break; case position.bottomleft: _stamppoint = new point (_Margin, _ioImageHeight - _iMageHeight - _Margin); Break;

case Position.BottomRight: _stampPoint = new Point (_iOImageWith - _margin - _iSImageWith, _iOImageHeight - _iSImageHeight - _margin); break; case Position.Center:_stampPoint = new Point (_iOImageWith / 2 - _iSImageWith / 2, _iOImageHeight / 2 - _iSImageHeight / 2 ); break; default: _stampPoint = new Point (_margin, _margin); break;} _graSettingImage.DrawImage (_bmStampImage, _stampPoint);} _graSettingImage.Dispose ();} catch (Exception e) {throw e;}} public void Save (String_TargetImageFile) {_bmoriginimage.save (_targetImagefile);} public void save (system.io.stream _stream) {_bmoriginimage.save (_Stream, Imageformat.jpeg);}

///

/// ///// public int margin {get {return_margin;} set {_margin = value;}} /// /// seal location /// public position stamppos {get {return _stamppos;} set {_stamppos = value;}} /// // need to add image files /// Public String OriginImageFile {get {return _originImageFile;} set {try {_originImageFile = value; _bmOriginImage = new Bitmap (_originImageFile); _dispose = false;} catch (Exception e) {throw e;}}} /// /// Need to add images /// public bitmap originimage {get {return _bmoriginimage;} set {_bmoriginimage = value; _dispose = false;}} /// /// seal image file path /// / public string stampimagefile {get {return _stampimagefile;} set {try {_stampimagefile = value; _BMSTAMPIMAGE = New bitmap (_stampimagefile); _dispose = false;} catch (exception e) {throw e;}

}}} ///

/// seal image /// public bitmap stampimage {get {return_bmstampimage;} set {_bmstampimage = value; _dispose = false;}} /// // / Seal text /// public string stampText {get {return _stamptext;} set {_stamptext = value;}} /// /// public font TextFont {get {return _TextFont;} set {_textfont = value;}} /// /// seal text color /// public color color textcolor {get {return_textcolor;} set {_textcolor = Value;}} /// /// seal mode, TRUE plus the Text seal, otherwise the image seal /// public bool textstampmode {get {return_textstampmode;} set {_textstampmode = value;} }}}

Below is an example of the use of an ASP.NET page:

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.Web.ui.htmlcontrols;

Namespace MySite {///

/// SampleImage Summary Description. /// public class sampleImage: System.Web.UI.Page {private void Page_Load (object sender, System.EventArgs e) {Response.ContentType = "image / jpeg"; string _sampleImageFile = Server.MapPath ( " sample.jpg "); string _stampImageFile = Server.MapPath (" stamp.jpg "); string _stampText = Request.Params [" Text "]; string _stampPos = Request.Params [" Pos "]; string _stamper = Request.Params ["Stamp"]; stamperimage _stamperImper;

IF (_stamper == null) _stamper = ""; if (_stamptext == "|| _stamptext == null) _stamptext =" Hello, World! "; if (_stamppos ==" || _stamppos == null) _stamppos = "1"; if (_stamper.ToLower () = "on"!) {_stamperImage = new StamperImage (_sampleImageFile, _stampText, Color.White);} else {_stamperImage = new StamperImage (_sampleImageFile, _stampImageFile);} _stamperImage.StampPos = (StamperImage.Position) Convert.Toint16 (_stamppos); _StamperImage.Stamper (); _StamperImage.Save (response.outputstream); _stamperimage.dispose ();}

#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// private void initializecomponent () {this.load = new system.eventhandler (this.page_load);} #ENDREGON}}

转载请注明原文地址:https://www.9cbs.com/read-6966.html

New Post(0)