If you want to design a unique ICO picture yourself, let it become a "My Computer", "Recycle Bin", what does this? Only some specialized drawings tools. Because the drawing program of Windows cannot create an ICO file. So I used .NET and GDI to write a drawing tool. Although there are many articles now introduce GDI technology, but it is just a simple application of pure GDI , at least I haven't seen an article using GDI development of a full software or program fragment.
This program implements the following features: transform BMP, JPG, JPEG, GIF, .png, .tiff file into ICO files, you can edit the transformed files; create and edit a new ICO file; for existing The ICO file is edited. All edited files are saved as an ICO file that can be used in any place where you can use the ICO file.
I will explain what is GDI . GDI is a subsequent version of GDI (earlier version of Windows), which is a subsequent version of graphic display technology, Microsoft Windows XP operating system. It has been integrated into the .NET development environment, so no matter what version is your OS, as long as the .NET framework is installed, there is GDI (note: is a .net frame, instead of .NET development environment, so Win98 can also Use GDI ). Of course, it also provides a traditional API that can be called by .NET or non-.NET development tool. Because he and GDI have a big difference, you must use GDI must be learned from his head. GDI is much more simpler than GDI.
Take a look at how to implement this software: Add PictureBox, 0penfileDialog, SaveFileDialog, ColorDialog, DomainUpdown, Label control; then add two menus, their submenu, the added menu, the "File" menu, including "New", " Open, "Save", "Exit", "Function" menu, including "straight line", "Select Color" code as follows, give the program instructions after the code:
Public Class Form1Inherits System.Windows.Forms.FormPublic imagepen, newbit, changiamge, mpen 'movepen, moveb ,, grh, filenames, endpenDim xd, yd, xu, yu, pk, psPrivate Sub MenuItem9_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem9.Click' ico create a new file, i.e., the "New" menu PictureBox1.Image = Nothing Dim bitnew As New System.Drawing.Bitmap (32, 32, Drawing.Imaging.PixelFormat.Format32bppArgb) ' Create a Bitmap object to draw Dim x, y for x = 0 to 31 for y = 0 to 31 BitNew.Setpixel (x, y, color.transparent) 'Set Bitmap's background to transparent next next new newbit = bitnew MenuItem3.Enabled = False ' "color select" menu is unavailable MenuItem2.Enabled = True' available End SubPrivate Sub MenuItem6_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem6.Click' "linear" menu Open the image file, "Open" menu "OpenFiledialog1.filter =" Ico file (*.,) | * .Ico | image file (* .bmp; *. Jpg; *. Jpeg; *. .tiff) | * .BMP; *. jpg; *. jpeg; *. gif; *. png; *. TIFF "openfiledialog1.filterIndex = 2 openfiledialog1.showdialog () openfiledialog1.filename =" "End Subprivate Sub M enuItem8_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem8.Click Me.Close ()' Exit End SubPrivate Sub MenuItem7_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem7 .Click 'saves files, "Save" Dialog Basket PictureBox1.cursor = System.Windows.Forms.cursors.default SaveFileDialog1.filter = "Ico file (* .ICO) | *." Set to save the file suffix SaveFileDialog1 .Showdialog () if SavefileDialog1.FileName <> "" "" The IF not savefiledialog1.showdialog.cancel kil dam bmp as new system.drawing.bitmap (PictureBox1.image, 32, 32) '
From PictureBox1.Image, Bitmap is initialized, and the settings saved as the picture are constructed from 32 * 32 and 16 * 16 format, here is 32 * 32, you can also set to 16 * 16 DIM ICO as system. Drawing.icon = ico.fromhandle (bmp.gethicon ()) 'With Bitmap handle, initialize icon, he is a class Dim file as new system.io.filestream (SaveFileDialog1.FileName) dedicated to the ICO file (SaveFileDialog1.FileName (), IO.FILEMODE .Create) 'Creating file streams ico.save (file)' Save As ICO file file.close () 'Close stream end if end ifend subslication submenem2_click (Byval Sender as system.Object, ByVal e as system.eventargs)' Handles MenuItem2.Click 'is a straight line in the drawing in the new ico PictureBox1.Cursor = System.Windows.Forms.Cursors.Cross' PictureBox1 mouse in the style ColorDialog1.ShowDialog () Dim pen As New Pen (ColorDialog1.Color, DomainUpDown1. Text ()) 'Create a brush imagepen = penEnd SubPrivate Sub PictureBox1_MouseDown (ByVal sender as System.Object, ByVal e as System.Windows.Forms.MouseEventArgs)' Handles PictureBox1.MouseDown 'Get if starting line when the left mouse button is pressed E.Button = MouseButtons.left the xd = ex / 8: YD = EY / 8 End IFEnd Subprivate Sub PictureBox1_mouseup (Byvale AS System.Object, Byval E AS System.Windows.Forms.Mou Seeventargs) 'Handles PictureBox1.mouseup' Painted Line IF PictureBox1.cursor is System.Windows.Forms.cursors.cross and PS <> 1 THEN XU = EX: YU = Ey Me.k (1, Imagepen, YU / 8, XU / 8, XD, YD) ELSE IF OpenFiledialog1.FilterIndex = 1 THEN XU = EX: YU = Ey Me.K (2, Mpen, YU / 8, XU / 8, XD, YD) End IFEnd Subpublic SUB K (ByVal k As Integer, ByVal drawtool As Object, ByVal x As Integer, ByVal y As Integer, ByVal xs As Integer, ByVal ys As Integer) If k = 1 Then PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage 'automatic image receiving PictureBox1.Image =
newbit Dim Graphic As Graphics Graphic = Graphic.FromImage (Me.PictureBox1.Image) 'in the drawing Graphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias PictureBox1' serrated cutting edge Graphic.DrawLine (drawtool, y, x, xs, ys ) 'objects End If If k = 2 Then PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage PictureBox1.Image = changiamge Dim Graphic As Graphics Graphic = Graphic.FromImage (Me.PictureBox1.Image) Graphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias Graphic.DrawLine (drawtool, y, x, xs, ys) End IfEnd SubPrivate Sub MenuItem3_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem3.Click' on an open file ico ColorDialog1 drawing a straight line. ShowDialog () Dim m3pen As New Pen (ColorDialog1.Color, DomainUpDown1.Text ()) 'establishment brush mpen = m3penEnd SubPrivate Sub OpenFileDialog1_FileOk (ByVal sender As Object, ByVal e AsSystem.ComponentModel.CancelEventArgs)' Handles OpenFileDialog1.FileOk 'open file IF OpenFiledialog1.FilterIndex = 1 THEN DIM M3PEN AS New Pen (Color.Black, Domainu pDown1.Text ()) mpen = m3pen MenuItem2.Enabled = False MenuItem3.Enabled = True Else MenuItem3.Enabled = False MenuItem2.Enabled = False End If If OpenFileDialog1.FileName <> "" Then PictureBox1.Cursor = System.Windows.Forms .Cursors.Default Dim images As New System.Drawing.Bitmap (OpenFileDialog1.FileName) changiamge = images PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage PictureBox1.Image = images Me.Text = OpenFileDialog1.FileName End IfEnd SubPrivate Sub Form1_Load (ByVal sender As Object BYVAL E as system.eventargs) 'Handles mybase.load' Due to just running the program,
There is no open ICO file and the newly established ICO object so you can't create a drawing tool object MenuItem3.enabled = false meneuitem2.enabled = false, the program:
1. How to create a new ICO file: first initialize Bitmap, then create a brush in the "Function" - "Line" menu code, you can start painting. At this point, just created a Bitmap object, which is drawn in Picturebox. After the painting, save the BitMap object to the file, complete the new ICO file.
2. How to open an existing ICO file and modify it: Judge whether the open file is ICO, if not only show him, if you are displayed and initialize a brush, pass the "function" - "" Select Color " To change the color and width of the drawing, then save, complete the modification of the original ICO file.
3. Save the file and convert the non-ICO file to an Ico file: By opening the file, the non-ICO file is displayed in Picturebox, and the Bitmap object is initialized by PictureBox.Image. The actual function of this sentence is to attach the current PictureBox.Image content Give Bitmap. With Bitmap's handle initial ICON object (object to handle ICO files), the role is to convert non-ICO files to an ICO file, establish a file stream object, and specify a new file name, and access method (file stream is the parameter of the Save method) Save the SAVE of the ICON object, and finally turn off the file stream.
4. How to draw: When 1 or 2 is completed, you can start the drawing, the drawing is implemented by the Sub K process, mouse-down, mouse-up. At this point, Mouse-Down is called to get the starting point of the straight line. After the mouse-up, the line end point is obtained, followed by calling the SUB K over mid-range binding Bitmap object to PictureBox in the mouse-Up, his effect is similar to one Zhang You can draw paper, and create a Graphics object with a graphic.fromimage (me.picturebox1.image) statement in Sub K, which is painted in PictureBox1.Image, not in PictureBox1, and their difference Where the former can save the results of the draw, the latter is not. K 's value is a newly created ICO file or modify the ICO file (k = 2 is a modified ICO file)
5. Some statement description: DIM PEN ... is to paint with a pen, Object.rawline (....) Means drawing a straight line.
6. Conversion of file format: You can use Image object's Save to convert image formats, but I found although he provided an ICON format, but it was not an ICO file after transformation, but a PNG file. From the web data shows this .NET itself problem. By the way, it is possible to use the Image object without constructor. Although it is scheduled to be inherited, it can actually do it. To use it to construct it with his fromFile or fromstream method.
7. Question: When you understand this article, you will be present in the PictureBox1.SizeMode = PictureBoxSizeMode.SizeMode = PictureBoxSizeMode.stretchImage, PictureBox1.Image = CHANGIAMGE This two code cannot be open to the number behind it. In other places, if k = 1 is placed in the Code section of the New "menu, k = 2 is in the IF statement after Else in mouseup! In fact, these two sentences are the biggest problems I have encountered when writing this program. I have used two hours of this two code to put in the present position. Finally, I have drawn three conclusions after discussing with friends: 1. .Net itself.
2. If the PictureBox1.Image object is lost (PictureBox1.Image is a Bitmap object), it cannot be bound to Graphics.
3. PictureBox1.Image object is not visible in Sub K. Although I don't know that the conclusion is right, I wrote it out, for reference only.
See MSDN for the use of 0penfileDialog, SavefileDialog, ColorDialog, DomainUpdown, and file streams in the program. These five are just to help this program. If you want to know here, this article is too long, and these use is simple. The drawing tool I used in the program is a pen. The picture drawing is a straight line. The ICO file is already enough. If you want to use other tools, draw other graphics, just modify the submenus in "Features", and SUB K code is enough.
Run as shown:
Replacement "My Computer" icon