Realization of digital watermarking tool based on C #
SummaryBased on the image provided by C #, it is easy to realize digital watermarking; here is a way to make a release tool.
Photo AddUsing C # 's FolderBrowserDialog class, you can easily read image files. This article is based on a directory to add photos, so the need for directory traversal. If you open a directory, you also need to open the subdirectory to read the photo; In this article, only JPG files are read, and try to read them before adding, to avoid some pictures can not read, affect the effectiveness of subsequent operations. In addition, after getting the path from the FolderBrowserDialog class, do not forget to read the file in the directory, which is an easy to ignore small problem, namely Get_file (Fbdlg. SelectedPath) and then call Get_dir (Fbdlg. SelectedPath).
private void Get_dir (string parent) { DirectoryInfo di = new DirectoryInfo (parent); foreach (DirectoryInfo subdir in Di. GetDirectories ()) { Get_file (subdir. FullName); } } private void Get_file (string parent) { DirectoryInfo di = new DirectoryInfo ( Parent); foreach (FileInfo fi in Di. GetFiles ())        &Nbsp; { if (FI. Extension.toupper () = = ". JPG ") { try { Image test = Image.FromFile (FI. FullName); if (test!= null) { & Nbsp; insertphoto (FI); test. Dispose (); } } catch (outofmemoryexception ome) { MessageBox.Show (fI.fullname + "can not being added into album" + Environment.NewLine + "reason[" + ome. Message + "]"); } catch (FileNotFoundException fnfe) { MessageBox.Show (FI. FullName + "can not being added into album" + Environment.NewLine + "reason[" + FNFE. Message + "]"); } catch (externalexception) { continue; } } } } |
The next question is: Read the photos, how to display. Given that the tool is not a picture-processing, the requirement scope is easier to determine, and the thumbnail is the best choice. Think of thumbnails, think of using ListView to display the added picture: largeimagelist just meet the requirements, in addition, ListViewItem checkbox is used to select the picture.
this. listview1.largeimagelist = il; for (int i = 0; i < il. Images.count; i++) { ListViewItem LVI = new ListViewItem (); LVI. Name = new FileInfo (Img_files[i]). Name; LVI. ImageIndex = i; This.listView1.Items.Add (LVI); } |
Picture SelectionListView's ListViewItem has a property named Checked that, as long as you change this attribute, you can select all and deselect all.
private void Selectalltoolstripmenuitem_click (object sender, EventArgs e) { foreach (ListViewItem lvi in This.listView1.Items) { if (!lvi. Checked) { Lvi. Checked = true; } } } private void Unselectalltoolstripmenuitem_click (object sender, EventArgs e) { foreach (ListViewItem lvi In This.listView1.Items) { if (LVI. Checked) { Lvi. Checked = false; } } } |
Watermark CustomizationCharacter watermarks can change the size, color, font name, and watermark content of the font; This provides a custom modal dialog box. There is a RichTextBox in the dialog box to show the actual effect of the customization; the FontDialog class is used, of course.
FontDialog Fdlg = new FontDialog (); Fdlg. Font = M_font; Fdlg. Showcolor = true; fdlg. Showeffects = true; if (DialogResult.OK = = Fdlg. ShowDialog ()) { This.richTextBox1.Font = Fdlg. font; This.richTextBox1.ForeColor = Fdlg . color; } Fdlg. Dispose (); |
When the dialog box presses the OK button, the custom information needs to be saved and persisted; This article uses the registry (see the article based on the C # Operations registry for registry operations).
Tips: About the implementation of modal dialog boxes. In C #, only the form class can be implemented by specifying parent when calling ShowDialog. In addition, you need to specify the corresponding return value in the dialog box to determine and cancel the DialogResult property of the button. |
Watermark AddThe process of adding a watermark is somewhat like a Photoshop layer: 1 A same size of bitmap;new 2) Copy picture properties, such as resolution information, date, etc. 3, first paint the layer white, 4, then the original picture on the above, 5 and then write the text on the above, 6 to save.
private void Watermarkphoto (string filename, Image img) { Bitmap B = new Bitmap (img. Width, IMG. Height, Pixelformat.format32bpprgb); b.setresolution (img. HorizontalResolution, IMG. VerticalResolution); foreach (PropertyItem item in IMG). PropertyItems) { B.setpropertyitem (item); } Graphics g = graphics.fromimage (b); G.clear (Color.whiTE); G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High; g.drawimage (img, 0, 0, img.) Width, IMG. Height); SolidBrush brush = new SolidBrush (m_default_ color); stringformat format = new StringFormat (stringformatflags.noclip); format. LineAlignment = Stringalignment.near; format. Alignment = stringalignment.near; &NBSP;&NBSP;&NBSP;&Nbsp; SizeF stringsize = g.measurestring (M_defalut_watermark, M_default_ Font); g.drawstring (M_defalut_watermark, M_default_ Font, brush, New PointF (IMG. Width-stringsize.width)/2, IMG. Height *2/3), format); try { b.save (filename, imageformat.jpeg); } catch (externalexception ee) { MessageBox.Show ("fail to handle" + filename + "because" + EE. message); } B. Dispose (); brush. Dispose (); g.dispose (); } |
Of course, change the font to calculate the watermark width.
SizeF stringsize = g.measurestring (M_defalut_watermark, M_default_font); |
Concluding remarksBased on the above thought, the realization of digital watermarking tool has been released to several friends to use, reflecting good. With its own watermark tool, the display of photos will not worry about infringement.