Relevant precautions and common problems of .NET drawing

1. .net is a drawing done by calling the API. Generally, you can draw on controls and forms, and you can also draw in bitmap. Bitmap inherits from Image

2. Drawing text in a transparent bitmap project, that is, the drawstring method, will generate black borders, but this problem can be solved by setting the background color

For example g.clear(color.black)

3. A very strange problem (already solved)

Sometimes we don't want to start ps to process pictures, but I already have applications for picture processing, but the pictures I export can't be recognized by pr, ae and other software, it tells me that the header file of the file is damaged

Solution: Export a picture of type png. Of course, if you are free, you can open winhex to detect that the header file starts with 89 50 4E 47. He said ?PNG

4. Make the picturebox refresh in real time, and make the illusion that I am editing

Can be added in the picturebox mouse move event

Pic.Invalidate()
Pic.Update()

5. How to do animation in .net?

I don't know if you have heard of Gif.Components, it can help you solve the problem ( third party )

For example:

Protected Image As Imaging.FrameDimension
    Protected bmp As Bitmap
    Protected bmp2() As Bitmap
    Public AGE As New Gif.Components.AnimatedGifEncoder()

    Public Function SplitGif(gif As String, SavePath As String, SaveName As String, SaveStyle As String)
        bmp = New Bitmap(gif)
        Image = New Imaging.FrameDimension(bmp.FrameDimensionsList(0))
        Dim bmp2(bmp.GetFrameCount(Image) - 1)
        For i As Integer = 0 To bmp2.Count - 1
            bmp.SelectActiveFrame(Image, i)
            bmp2(i) = New Bitmap(bmp)
        Next
        For i As Integer = 0 To bmp2.Count - 1
            bmp2(i).Save(SavePath & "\" & SaveName & i & "." & SaveStyle)
        Next
        Return vbNull
    End Function

    Public Function CraftGif(sleep As Integer, pics As String(), out As String)

        Dim count As Integer
        count = pics.Count

        AGE.Start(out)
        AGE.SetDelay(sleep)
        AGE.SetRepeat(0)
        Dim i As Integer = 0
        While i < count
            AGE.AddFrame(Drawing.Image.FromFile(pics(i)))
            Math.Max(Threading.Interlocked.Increment(i), i - 1)
        End While
        AGE.Finish()
        Return vbNull
    End Function

6. How to solve the problem Open the picture by yourself, I want to replace the original picture, but the picture is occupied

It's very simple, first load it to the stream, then assign a value to the bitmap, and finally release the stream.

For example:

Dim op As New OpenFileDialog
op.Filter = "All .net Picture Files|*.jpg;*.png;*.bmp;*.ico;*.jpeg;*.*"
op.Title = "选择一个图片"
If op.ShowDialog() = DialogResult.OK Then
    Dim fs As New FileStream(op.FileName, FileMode.Open, FileAccess.Read)
    Dim bmp As Bitmap = Image.FromStream(fs)
    Form3.pic = bmp
    Form3.ShowDialog()
    Pic.Location = New Point(0, 0)
    fs.Close()
End If

The form3 setting allows the user to choose the picture editing method (you can ignore this step)

Then you can replace the original file with

Guess you like

Origin blog.csdn.net/weixin_56050945/article/details/128490182