首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

在picturebox里面通过鼠标移动事件画好的图(好像不是image)?那小弟我该如何保存成图片格式呢?跪求,纠结好久了这个有关问题!多谢了

2012-12-22 
在picturebox里面通过鼠标移动事件画好的图(好像不是image)?那我该怎么保存成图片格式呢?????跪求,纠结好

在picturebox里面通过鼠标移动事件画好的图(好像不是image)?那我该怎么保存成图片格式呢?????跪求,纠结好久了这个问题!谢谢了
在picturebox里面通过鼠标移动事件画好的图?可以保存成图片格式吗?????
[解决办法]
绘制的时候,通过bitmap绘图
Bitmap bmp = new Bitmap(100, 100);
using(Graphics g = Graphics.FromImage(bmp))
{
  .....
  ......
}
string file = "image.jpg";
bmp.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg);
[解决办法]
这是我写的代码,保存之后只是一张空白图片……而没有我画的曲线啊??帮我看看
Public Class Form1
    Dim preX As Single
    Dim preY As Single
    Private pStart, pEnd As Point
    Dim ep As New Pen(Color.Red, 1)
    Public image As Bitmap = Nothing
    'Dim bm As New Bitmap(500, 500)
    'Dim bm = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height, Me.PictureBox1.CreateGraphics)
    'Dim g = Graphics.FromImage(bm)
    Dim g As Graphics
    Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Me.PictureBox1.Controls.Clear()
        image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        Graphics.FromImage(image).Clear(Color.White)
        Me.PictureBox1.Image = DirectCast(image.Clone(), Bitmap)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            pStart.X = e.X
            pStart.Y = e.Y
        End If

    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        Using gr As Graphics = Graphics.FromImage(image)
            Dim g As Graphics = PictureBox1.CreateGraphics()
            If e.Button = Windows.Forms.MouseButtons.Left Then
                pEnd.X = e.X
                pEnd.Y = e.Y
                g.DrawLine(ep, pStart, pEnd)
                ListBox1.Items.Add(pStart.X & " " & pStart.Y)
                pStart = pEnd
            End If


        End Using
        ' PictureBox1.Image. = bm


    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image.Save("C:\Users\Administrator\Desktop\123.jpeg")
    End Sub
[解决办法]


Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Public Class Form2
    Inherits Form

    'Dim bmp1 = New Bitmap(PictureBox1.Width, PictureBox1.Height, PictureBox1.CreateGraphics)
    ''产生画布
    'Dim mygraphics1 = Graphics.FromImage(bmp1)

    ''定义画笔1
    'Dim mypen1 As New Pen(Brushes.Green, 2)
    

    Public Sub New()
        InitializeComponent()
    End Sub
    '定义画线的两个点p1起始点p2终点
    Private p1 As Point, p2 As Point
    '定义存储绘过的所有对象(窗体刷新时用来重绘)
    Private ls As New List(Of drawtype)()
    Public image As Bitmap = Nothing
    '窗体载入时新建一个绘图对象
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        'mygraphics1.Clear(Color.Black)
        'mygraphics1.DrawLine(mypen1, 50, 150, 550, 150)
        'PictureBox1.Image = bmp1


        Me.PictureBox1.Controls.Clear()
        image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        Graphics.FromImage(image).Clear(Color.White)
        '消除底图的黑色 
        Me.PictureBox1.Image = DirectCast(image.Clone(), Bitmap)
        '这句话是关键 
    End Sub

    '点击保存图片
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        'PictureBox1.Invalidate()
        Dim sf As New SaveFileDialog()
        sf.Filter = "(*.bmp)
[解决办法]
*.bmp
[解决办法]
(*.jpg)
[解决办法]
*.jpg"
        sf.FilterIndex = 2


        sf.RestoreDirectory = True
        If sf.ShowDialog() = DialogResult.OK Then
            'PictureBox1.Image.Save(sf.FileName)

            Dim bmp As New Bitmap(PictureBox1.Size.Width, PictureBox1.Size.Height)
            PictureBox1.DrawToBitmap(bmp, New Rectangle(New Point(0, 0), PictureBox1.Size))
            bmp.Save(sf.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)

        End If
    End Sub
    '当鼠标点击窗体时记录坐标点(用来记录直线的第一个坐标)
    Private Sub pictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        '开始记录坐标
        p1 = e.Location
    End Sub
    '鼠标在窗体中按下时的绘图过程
    Private Sub pictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
        Dim g As Graphics = PictureBox1.CreateGraphics()
        '如果鼠标左键一直接下
        If e.Button = MouseButtons.Left Then
            '开始画白线(与背景色一样的线,用来擦除鼠标移动时产生的线)
            g.DrawLine(New Pen(Color.White, 2), p1, p2)
            '获得鼠标在窗体中按下并移动时的坐标点
            p2 = e.Location
            '在窗体中画出的线
            g.DrawLine(New Pen(Color.Blue, 2), p1, p2)
            '使用枚举将所有画过的线重绘(为了防止新画的线的白色会将原有线更改)
            chonghui()
        End If
    End Sub
    Private Sub chonghui()
        Dim g As Graphics = PictureBox1.CreateGraphics()
        For Each t As drawtype In ls
            '画出原有的每一条线
            g.DrawLine(New Pen(Color.Blue, 2), t.p1, t.p2)
        Next
    End Sub
    '鼠标点击弹起时,记录刚刚画线的位置
    Private Sub pictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
        '记录画线的位置,放到泛型集合中
        ls.Add(New drawtype(p1, New Point(e.X, e.Y)))
    End Sub

    Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint


         For Each t As drawtype In ls
            '画出原有的每一条线
            e.Graphics.DrawLine(Pens.Blue, t.p1, t.p2)
        Next

    End Sub

End Class
'定义存储画线动作的类
Class drawtype
    '定义两个点
    Public p1 As Point, p2 As Point
    '定义构造方法(两个点为参数)
    Public Sub New(ByVal pp As Point, ByVal pp2 As Point)
        p1 = pp
        p2 = pp2
    End Sub
End Class



这是完整版,你参考着改吧  关键在于 
 e.Graphics.DrawLine(Pens.Blue, t.p1, t.p2)
[解决办法]
亲,您给我的这个完整版保存过后,是图片格式吗??好像不是哎,很感谢您,请问您自己看过效果吗????如果您的效果可以实现的话,我的就可以实现……再帮帮忙……
[解决办法]
不是jpg 与 BMP 都可以选吗?

 bmp.Save(sf.FileName, System.Drawing.Imaging.ImageFormat.Jpeg) 
你可以改为 
 bmp.Save(sf.FileName, System.Drawing.Imaging.ImageFormat.Bmp) 


我试过,BMP比较清晰。JPEG因为压缩过,有噪点 
[解决办法]
Imports System.Drawing.Drawing2D
Public Class Form1
    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click

        Dim bmp = New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
        Dim g = Graphics.FromImage(bmp)
        g.FillRectangle(New SolidBrush(Color.Gainsboro), 0, 0, PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
        g.DrawLine(New Pen(Color.Blue, 2.0F), 60, 90, 80, 140)
        PictureBox1.Image = bmp
        bmp.Save(System.Environment.CurrentDirectory & "\MY1.jpg")
        Me.PictureBox1.CreateGraphics.DrawLine(New Pen(Color.DarkGray, 3), 37, 40, 80, 60)
        Dim p(3) As Point

        p(0).X = 100

        p(0).Y = 15

        p(1).X = 80

        p(1).Y = 90

        p(2).X = 90

        p(2).Y = 120

        p(3).X = 130

        p(3).Y = 150
        Me.PictureBox1.CreateGraphics.DrawPolygon(New Pen(Color.DarkGray, 3), p)
        Dim dc1 As Graphics = Me.PictureBox1.CreateGraphics


        dc1.DrawLine(New Pen(Color.DarkGray, 3), 37, 140, 180, 60)
        g.DrawPolygon(New Pen(Color.DarkGray, 3), p)
        g.DrawLine(New Pen(Color.DarkGray, 3), 37, 140, 180, 60)

    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        Try
            Me.PictureBox1.Image.Save(System.Environment.CurrentDirectory & "\MY.jpg")
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("PictureBoX中没有图形", "程序提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
        End Try
        If Me.PictureBox1.Image IsNot Nothing Then
            MessageBox.Show("不空")
        Else
            MessageBox.Show("空")
        End If
    End Sub
End Class
新建一个工程,在窗体上添加一个图片框、两个按钮
[解决办法]
亲,谢谢您给我的代码!您给的这个代码,应该可以实现,但是我的情况,好像和您的不太一样,我的是在运行代码之后,在picturebox里面画线,您的这个是直接用按钮点击,实现画图,二者实现起来是有差别的!!您觉得一样吗???
[解决办法]
谢过了,我怎么感觉我的代码还是没法儿实现啊??我又编了一上午!!还是不行啊
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()
    End Sub
    '定义画线的两个点p1起始点p2终点
    Private p1 As Point, p2 As Point
    Public image As Bitmap = Nothing
    '窗体载入时新建一个绘图对象 

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.PictureBox1.Controls.Clear()
        image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        Graphics.FromImage(image).Clear(Color.White)
        '消除底图的黑色
        Me.PictureBox1.Image = DirectCast(image.Clone(), Bitmap)
        '这句话是关键  
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sf As New SaveFileDialog()
        sf.Filter = "(*.bmp)


[解决办法]
*.bmp
[解决办法]
(*.jpg)

热点排行