System.ArgumentException HResult=0x80070057 Message=使用されたパラメーターが有効ではありません。 Source=System.Drawing スタック トレース: at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at 勉強.文字編集Form.Button10_Click(Object sender, EventArgs e) in C:\Users\userMM\OneDrive\ソフト\勉強\文字編集Form.vb:line 736 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at 勉強.My.MyApplication.Main(String[] Args) in :line 83
探した所先のURLの If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose() End If
実際は上記を参考に下記の様に書き開けてます Dim Rbox() As Global.System.Windows.Forms.RichTextBox = {RichTextBox1, RichTextBox2, RichTextBox3, RichTextBox4, RichTextBox5} Dim bmp = CreateBitmapFromControl(Rbox(PageNo)) If PictureBox16.Image IsNot Nothing Then ' PictureBox16.Image.Dispose() End If PictureBox16.Image = bmp 'ピクチャ1−15へ各個転送 Dim Picture() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, ・・・・・PictureBox15} Picture(PageNo).Image = PictureBox16.Image
For i As Byte = 0 To 4 Picture(i).Size = New Size(Picture(i).Size.Width, 320) Picture(i).Size = New Size(Picture(i).Size.Height, 160) Picture(i).SizeMode = PictureBoxSizeMode.Zoom Picture(i).Image = Image.FromFile(PicturePath + "MojiGAZou" + CStr(i + 1) + ".bmp") Picture(i).Dispose() Next
> 実際は上記を参考に下記の様に書き開けてます > Dim Rbox() As Global.System.Windows.Forms.RichTextBox = > {RichTextBox1, RichTextBox2, RichTextBox3, RichTextBox4, RichTextBox5} 繰り返し使うのであれば、フィールド変数にしておいた方が良いかも。
Public Class Form1 Private Rbox As RichTextBox() Private Picture As PictureBox () Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Rbox = {RichTextBox1, RichTextBox2, RichTextBox3, RichTextBox4, RichTextBox5} Picture = {PictureBox1, PictureBox2, PictureBox3, …, PictureBox15} End Sub End Class
本題ですが先のコードでわDisposeした方が良いのか程度で書いてました Form LoadでDisposeしてますがボタンクリックでTextBoxから画像を生成して各PictureBoxに書いてますので問題ないと解釈していました 今二つの問題に当たっています。スレッドを分けなかったのは同じ要因かなと思ってです。
Dim Picture() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5} For i As Byte = 0 To 4 Picture(i).Size = New Size(Picture(i).Size.Width, 320) Picture(i).Size = New Size(Picture(i).Size.Height, 160) Picture(i).SizeMode = PictureBoxSizeMode.Zoom Picture(i).Image = Image.FromFile(PicturePath + "MojiGAZou" & CStr(i + 1) & ".bmp") Next
System.Runtime.InteropServices.ExternalException HResult=0x80004005 Message=GDI+ で汎用エラーが発生しました。 Source=System.Drawing スタック トレース: at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at Test.文字編集Form.Button18_Click(Object sender, EventArgs e) in C:\Users\userMM\OneDrive\Test\文字編集Form.vb:line 232 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at Test.My.MyApplication.Main(String[] Args) in :line 83
> If PictureBox16.Image IsNot Nothing Then > PictureBox16.Image.Dispose() > End If 上記は『PictureBox16.Image?.Dispose()』の一行で書けます。 https://learn.microsoft.com/ja-jp/dotnet/visual-basic/language-reference/operators/null-conditional-operators
> Dim Picture() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5} > For i As Byte = 0 To 4 配列のインデックスは常に Integer 型です。Byte 型ではありません。
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose() '処分 End If PictureBox1.Image = Nothing '解放
という意味です。
KOZ さんの書かれた No35282 の場合は、 Dim bmp = CreateBitmapFromControl(RichTextBox1) If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose() End If PictureBox1.Image = bmp ですね。