VB.NET中用drawImage()画图以后怎么保存?程序
代码如下:
Public Class Form1
Inherits Sy
stem.Windows.Forms.Form
Dim ofd As New SaveFileDialog
Private picture
Box1 As New Picture
Box
#Region Windows Form Designer generated code
Public Sub New()
MyBase.New()
This call is
required by the Windows Form Designer.
InitializeComponent()
Add any initialization after the InitializeComponent() call
End Sub
Form overrides
dispose to clean up the component list.
Protected Overloads Overrides Sub
dispose(ByVal
disposing As Boolean)
If
disposing Then
If Not (components Is
nothing) Then
components.
dispose()
End If
End If
MyBase.
dispose(
disposing)
End Sub
required by the Windows Form Designer
Private components As Sy
stem.ComponentModel.IContainer
NOTE: The following procedure is
required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
Friend WithEvents Picture
Box1 As Sy
stem.Windows.Forms.Picture
Box
Friend WithEvents MainMenu1 As Sy
stem.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As Sy
stem.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As Sy
stem.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As Sy
stem.Windows.Forms.MenuItem
Private Sub InitializeComponent() Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.SuspendLayout() PictureBox1 Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Fill Me.PictureBox1.Location = New System.Drawing.Point(0,0) Me.PictureBox1.Name = PictureBox1 Me.PictureBox1.Size = New System.Drawing.Size(392,358) Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False MainMenu1 Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) MenuItem1 Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2,Me.MenuItem3}) Me.MenuItem1.Text = file MenuItem2 Me.MenuItem2.Index = 0 Me.MenuItem2.Text = save MenuItem3 Me.MenuItem3.Index = 1 Me.MenuItem3.Text = Form1 Me.AutoScaleBaseSize = New System.Drawing.Size(6,14) Me.ClientSize = New System.Drawing.Size(392,358) Me.Controls.Add(Me.PictureBox1) Me.Menu = Me.MainMenu1 Me.Name = Form1 Me.Text = Form1 Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Dock the PictureBox to the form and set its background to white. pictureBox1.Dock = DockStyle.Fill pictureBox1.BackColor = Color.White pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage Connect the Paint event of the PictureBox to the event handling method. AddHandler pictureBox1.Paint,AddressOf Me.pictureBox1_Paint Add the PictureBox control to the Form. Me.Controls.Add(pictureBox1) End Sub Private Sub pictureBox1_Paint(ByVal sender As Object,ByVal e As System.Windows.Forms.PaintEventArgs) Create image. Dim newImage1 As Image = Image.FromFile( F:/lianxi/Image1--1.jpg ) Dim newImage2 As Image = Image.FromFile( F:/lianxi/Image1--2.jpg ) Dim newImage3 As Image = Image.FromFile( F:/lianxi/Image2--1.jpg ) Dim newImage4 As Image = Image.FromFile( F:/lianxi/Image2--2.jpg ) Create rectangle for displaying image. Dim destRect As New Rectangle(0,520,520) Create rectangle for source image. Dim srcRect As New Rectangle(0,520) Dim destRect1 As New Rectangle(520,520) Dim destRect2 As New Rectangle(0,520) Dim destRect3 As New Rectangle(520,520) Dim rec As Rectangle Dim units As GraphicsUnit = GraphicsUnit.Pixel Draw image to screen. e.Graphics.DrawImage(newImage1,destRect,srcRect,units) e.Graphics.DrawImage(newImage2,destRect1,units) e.Graphics.DrawImage(newImage3,destRect2,units) e.Graphics.DrawImage(newImage4,destRect3,units) End Sub pictureBox1_Paint Private Sub MenuItem2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MenuItem2.Click ofd.Filter = jpg file|*.jpg|bmp file|*.bmp Dim bmp As New Bitmap(1040,1040) bmp = Me.PictureBox1.Image If (ofd.ShowDialog = DialogResult.OK) Then bmp.Save(ofd.FileName) End If End Sub End Class 每次运行到 bmp.Save(ofd.FileName) 这一步时就出现An unhandled exception of type System.NullReferenceException occurred in WindowsApplication4.exe Additional information: Object reference not set to an instance of an object.查看了下,原来Me.PictureBox1.Image为nothing,这样传值给bmp时,bmp也是nothing.想了下,最终的问题归结为用DrawImage()函数以后,居然没有把图象画到PictureBox1中,但是可以在PictureBox1中显示,也不知道是怎么回事. 在线等回复. __________________________________________________________________________ 帮顶下,学习中.... __________________________________________________________________________ 看一下ofd是否为空引用。 然后bmp.Save(file) 中file参数应该是你要保存到的路径。 __________________________________________________________________________ Dim ofd As New SaveFileDialog这句已经定义,ofd不会为空,关键是bmp显示到了控件PictureBox1中,但是PictureBox1的属性项Image为空,也真够郁闷的,画了图象进去却不能保存. __________________________________________________________________________ bmp.save的时候第一个写名字,第二个一定要 imageformat.xx,即保存的格式,否则就保存为位图 __________________________________________________________________________ 低效 __________________________________________________________________________
原文地址:https://www.jb51.cc/vb/263703.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。