Visual Basic.NET and GDI + Create icon Editor

Source: Internet
Author: User
Tags filter exit integer save file versions
If you want to design a unique ICO image of your own, and then make it an icon like "My Computer" and "Recycle Bin", what do you do? Only some special drawing tools are used. Because the paint program for Windows cannot create an ICO file. So I used. NET and GDI + to write a drawing tool like this. Although there are many articles that introduce GDI + technology, but are just pure GDI + simple application of the introduction, at least I have not seen a use of GDI + to develop a complete software or program fragments of the article.

This program implements the following functions: Convert BMP, JPG, JPEG, GIF,. png,. tiff files into an ICO file, edit the converted file, create and edit a new Ico file, and edit an existing ICO file. All edited files are saved as ICO files and can be used wherever an ICO file can be used.

Let me first explain what GDI + is. GDI + is a subsequent version of GDI (the graphical device interface provided by Windows earlier versions) and is the graphical display technology for subsequent versions of Microsoft Windows XP operating systems. It has been integrated into the. NET development environment, so no matter what version of your OS is installed, there is GDI + as long as you install the. NET Framework (note: the. NET Framework, not the. NET development environment, so you can also use GDI + in Win98). Of course it also provides a traditional API that can be invoked by. NET or non. NET development tools. Because of the difference between his and the use of GDI, you have to learn from scratch to use GDI +. GDI + is much simpler than GDI.

Now let's take a look at how to implement this software: Add the Picturebox,0penfiledialog,savefiledialog,colordialog,domainupdown,label control first, and then add two menus, their submenus, Added menu The following "File" menu includes "new", "Open", "save", "exit", "function" menu includes "line", "Select Color" code as follows, give the program description after the code:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Imagepen, Newbit, Changiamge, MPen ' Movepen,moveb,,grh,filenames,endpen
Dim XD, yd, Xu, Yu, PK, PS

Private Sub Menuitem9_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) ' Handles Menuitem9.click
' Create a new ICO file, that is, ' new ' menu

pictureBox1.Image = Nothing
Dim bitnew as New System.Drawing.Bitmap (32, 32,
DRAWING.IMAGING.PIXELFORMAT.FORMAT32BPPARGB) ' Create a bitmap object to paint on it
Dim x, y
For x = 0 to 31
For y = 0 to 31
Bitnew. SetPixel (x, y, color.transparent) ' sets the bitmap background to Transparent
Next
Next

Newbit = Bitnew
menuitem3.enabled = False ' Choose Color ' menu not available
menuitem2.enabled = True ' line ' menu available
End Sub

Private Sub Menuitem6_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) ' Handles Menuitem6.click
' Open picture file ' opens ' menu '

Openfiledialog1.filter = "ico file (*.ico) |*.ico| image file
(*. bmp;*. jpg;*.jpeg;*. Gif;*.png;*.tiff) |*. bmp;*. jpg;*.jpeg;*. Gif;*.png;*.tiff "

Openfiledialog1.filterindex = 2
Openfiledialog1.showdialog ()
Openfiledialog1.filename = ""

End Sub

Private Sub Menuitem8_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) ' Handles Menuitem8.click

Me.close () ' Exit

End Sub

Private Sub Menuitem7_click (ByVal sender as System.Object,
ByVal e as System.EventArgs)

' Handles Menuitem7.click
' Save file, ' save ' dialog basket

Picturebox1.cursor = System.Windows.Forms.Cursors.Default
Savefiledialog1.filter = "ico file (*.ico) |*.ico" ' Sets the file suffix to save
Savefiledialog1.showdialog ()
If savefiledialog1.filename <> "" Then
If not SaveFileDialog1.ShowDialog.Cancel Then
Dim bmp as New System.Drawing.Bitmap (pictureBox1.Image,
32,32) ' Initializes the bitmap from the pictureBox1.Image, setting the size of the saved picture, the standard ICO by
32*32 and 16*16 Two formats, here for 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 that specializes in ICO files
Dim file as New System.IO.FileStream (Savefiledialog1.filename (),
Io. FileMode.Create) ' Create a file stream
Ico. Save (file) ' saved as an ICO file
File. Close () ' Closes the stream
End If
End If
End Sub

Public Sub Menuitem2_click (ByVal sender as System.Object,
ByVal e as System.EventArgs)

' Handles Menuitem2.click
' is drawing in a new ICO with a straight line

Picturebox1.cursor =system.windows.forms.cursors.cross
' The style of the mouse in the PictureBox1

Colordialog1.showdialog ()
Dim pen as New pen (colorDialog1.Color, Domainupdown1.text ()) ' Create brush
Imagepen = Pen

End Sub

Private Sub Picturebox1_mousedown (ByVal sender as System.Object,
ByVal e as System.Windows.Forms.MouseEventArgs)

' Handles Picturebox1.mousedown
' Gets the starting point of the line when the left mouse button is pressed

If E.button = MouseButtons.Left Then
XD = E.X/8: yd = E.Y/8
End If

End Sub

Private Sub Picturebox1_mouseup (ByVal sender as System.Object,
ByVal e as System.Windows.Forms.MouseEventArgs)

' Handles picturebox1.mouseup
' Draw a straight line
If Picturebox1.cursor is System.Windows.Forms.Cursors.Cross and PS <> 1 Then
Xu = E.x:yu = E.y
ME.K (1, Imagepen, YU/8, XU/8, XD, yd)
Else
If Openfiledialog1.filterindex = 1 Then
Xu = E.x:yu = E.y
ME.K (2, MPen, YU/8, XU/8, XD, yd)
End If
End If
End Sub

Public 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 ' Auto hold picture
pictureBox1.Image = Newbit
Dim Graphic as Graphics
Graphic = Graphic.fromimage (Me.PictureBox1.Image) ' paint on PictureBox1 '
Graphic.smoothingmode = Drawing.Drawing2D.SmoothingMode.AntiAlias ' jagged edges
Graphic.drawline (Drawtool, y, X, XS, YS) ' Draw line
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 If
End Sub

Private Sub Menuitem3_click (ByVal sender as System.Object,
ByVal e as System.EventArgs)

' Handles Menuitem3.click
' Draw a line with the open ICO file

Colordialog1.showdialog ()
Dim M3pen as New pen (colorDialog1.Color, Domainupdown1.text ()) ' Create a brush
MPen = M3pen

End Sub

Private Sub Openfiledialog1_fileok (ByVal sender as Object, ByVal e As
System.ComponentModel.CancelEventArgs)

' Handles Openfiledialog1.fileok
' Open File

If Openfiledialog1.filterindex = 1 Then
Dim M3pen as New Pen (Color.Black, Domainupdown1.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 If

End Sub

Private Sub Form1_Load (ByVal sender as Object, ByVal e as System.EventArgs)

' Handles MyBase.Load
' You cannot create a drawing tool object because you have not opened an ICO file and a newly created ICO object because you just ran the program.

menuitem3.enabled = False
menuitem2.enabled = False

End Sub
End Class

Program Description:

1. How to create a new ICO file: First initialize the bitmap, and then in the "function"-"line" menu code in the creation of a brush, you can start painting. This is just the creation of a bitmap object that we draw in the PictureBox. When you finish drawing the bitmap object to a file, you complete the new ICO file.

2. How to open an existing ICO file, and modify it to save it: Determine if the open file is ICO, if not just show him, if it is to show and initialize a brush, through the "function"-"Choose color" to change the color and width of the drawing line, and 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 the PictureBox. The actual effect of this sentence is to attach the current picturebox.image content to bitmap with the Picturebox.image initialization of the bitmap object. The initial Icon object (the object that processes the ICO file) with the bitmap handle is to convert a non-ICO file into an ICO file, create a file stream object, specify a new file name in it, and access methods (the file stream is the parameter of the Save method) are saved using the Icon object's Save, Finally, closes the file stream.

4. How to draw: when finished 1 or 2, you can start drawing, drawing is by sub K process, mouse-down,mouse-up to achieve. Call Mouse-down to get the starting point of the line, get the line end in mouse-up, and then call Sub K over medium-range bound bitmap object to PictureBox Image property in Mouse-up, which is similar to having a piece of paper that can be painted, The Graphics object is created with the Graphic.fromimage (Me.PictureBox1.Image) statement in Sub K, The expression is in the pictureBox1.Image bitmap object painting, instead of painting on the PictureBox1, their difference is that the former can save the painting results, the latter cannot. The value of K indicates whether to paint or modify an ICO file in the new ICO file (k=2 is the means to modify an existing ICO file)

5. Some statement description: Dim pen ... It means to draw with a pen, object.rawline (...) to draw a straight line,

6. File format conversion problem: You can use the Image object's Save method to convert the image format, but I found that although he provided the icon format, but the conversion is not an ICO file, but PNG file. Data from the Web show that this is the problem of. NET itself. By the way, the image object has no constructor, although he is marked as having to inherit before it can be used, but in practice it is not possible to use it to construct it with his fromfile or FromStream method.

7. Question about K: When you read this article you will certainly ask why the Picturebox1.sizemode = pictureboxsizemode.stretchimage,picturebox1.image in each branch Changiamge these two lines of code can not be separated from the code behind it, such as k=1 when placed in the "new" menu of the Code section, k=2 is placed in the MouseUp in the IF statement after the else! In fact, these two sentences is the biggest problem I encountered in writing this program, I spent two hours before the two code to put in the current position. Finally look at the data and discuss with friends to draw 3 conclusions:

1.. NET itself problem.

2. If the pictureBox1.Image object is lost (pictureBox1.Image returns a Bitmap object), it cannot be bound to graphics.

3. The pictureBox1.Image object is not visible in sub K. Although I do not know that the conclusion is right, but I wrote it out, for reference only.

For 0penfiledialog,savefiledialog,colordialog,domainupdown in your program, see MSDN for the use of file streams. These 5 are just for the purpose of supporting this program, and if you want to make it clear that this piece of paper is too long, and the use of these is very simple. I use the drawing tool in the program is a pen, drawing the graph is a straight line, this team ico file has enough, if you want to use other tools, draw other graphics, just modify the "function" in the submenu, and sub K code is enough.

Run as shown:



My Computer icon after the replacement






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.