I am currently trying to make an existing VB.NET Project run. A null pointer exception is thrown in the WndProc message but the stack trace does not really give me anything. I can place a break point in the startup form's designer code but when I step through it triggers a NULL pointer exception via WndProc function. The upper methods seems to be Windows methods. The only clue I have is the Message parameter with Msg = 24 and WParam = 1. I think the HWnd = 5178884 does not help.
I am pasting the stack trace in case someone has any ideas.
Note: I masked the MyNamespace and MyBaseForm and MyFormA because the source code is proprietary. This runs in Visual Studio 2008 on .NET Framework 3.5
MyNamespace.Forms.MyBaseForm.WndProc(Message& m)\r\n
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n
System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n
System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)\r\n
System.Windows.Forms.Control.SetVisibleCore(Boolean value)\r\n
System.Windows.Forms.Form.SetVisibleCore(Boolean value)\r\n
System.Windows.Forms.Control.set_Visible(Boolean value)\r\n
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n
System.Windows.Forms.Application.Run(ApplicationContext context)\r\n
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()\r\n
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()\r\n
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)\r\n
MyFormA.My.MyApplication.Main(String[] Args)
17d14f5c-a337-4978-8281-53493378c1071.vb:Line 81\r\n
System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n
System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n
System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n
System.Threading.ThreadHelper.ThreadStart()"
WM_SHOWWINDOW = 24
...not that it really helps.
It looks like the last bit of your code to be executed is line 81, I'd study that.
I just want to give another answer on how to know where the error came from even if the stack trace did not tell you anything (The actual problem why I asked what is WndProc 24). Just in case other people had the same problem too.
I used the Unhandled Exception via Debug > Exceptions menu on Visual Studio. See this link for detail.
Maintaining the point to C.Barlow as he answered the initial question.
Related
Let's start out by saying I'm not a terrifically experienced developer, so there is every possibility that I'm completely missing something here.
I'm implementing the Atlassian.NET SDK in a VB .Net solution. For the most part things work well, but I get the following error when I try to use the LINQ provider directly:
An unhandled exception of type 'System.InvalidCastException' occurred
in Atlassian.Jira.dll
Additional information: Unable to cast object of type
'System.Linq.Expressions.PropertyExpression' to type
'System.Linq.Expressions.ConstantExpression'.
The full exception:
System.InvalidCastException was unhandled HResult=-2147467262
Message=Unable to cast object of type
'System.Linq.Expressions.PropertyExpression' to type
'System.Linq.Expressions.ConstantExpression'. Source=Atlassian.Jira
StackTrace:
at Atlassian.Jira.Linq.JqlExpressionVisitor.GetFieldNameFromBinaryExpression(BinaryExpression
expression) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
65
at Atlassian.Jira.Linq.JqlExpressionVisitor.ProcessIndexedMemberEqualityOperator(BinaryExpression
expression, Boolean equal) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
142
at Atlassian.Jira.Linq.JqlExpressionVisitor.ProcessEqualityOperator(BinaryExpression
expression, Boolean equal) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
136
at Atlassian.Jira.Linq.JqlExpressionVisitor.VisitBinary(BinaryExpression
node) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
325
at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor
visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1
node)
at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression
node)
at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor
visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider
nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression
node)
at Atlassian.Jira.Linq.JqlExpressionVisitor.VisitMethodCall(MethodCallExpression
node) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
252
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor
visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider
nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression
node)
at Atlassian.Jira.Linq.JqlExpressionVisitor.VisitMethodCall(MethodCallExpression
node) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
252
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor
visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Atlassian.Jira.Linq.JqlExpressionVisitor.Process(Expression expression) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JqlExpressionVisitor.cs:line
42
at Atlassian.Jira.Linq.JiraQueryProvider.Execute(Expression expression, Boolean isEnumerable) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JiraQueryProvider.cs:line
44
at Atlassian.Jira.Linq.JiraQueryProvider.Execute(Expression expression) in
c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JiraQueryProvider.cs:line
39
at Atlassian.Jira.Linq.JiraQueryable`1.GetEnumerator() in c:\projects\atlassian-net-sdk\Atlassian.Jira\Linq\JiraQueryable.cs:line
29
at System.Linq.Buffer1..ctor(IEnumerable\1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at JiraUpdater.JiraStuff.GetIssueByDetail(String summary) in C:\Users\v709092\Documents\Visual Studio
2013\Projects\JiraUpdater\JiraUpdater\JiraStuff.vb:line 33
at JiraUpdater.Form1.ButtonMassUpdate_Click(Object sender, EventArgs e) in C:\Users\v709092\Documents\Visual Studio
2013\Projects\JiraUpdater\JiraUpdater\Form1.vb:line 98
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 JiraUpdater.My.MyApplication.Main(String[] Args) in :line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() InnerException:
My code is a straight forward conversion of sample code found on the project WIKI page.
Dim myJira As Jira = Jira.CreateRestClient("JiraServer", "username", "password")
Dim myIssueQuery = From i In myJira.Issues
Where i.Project = "JiraProjectName"
Select i
Console.WriteLine( myIssueQuery.Count())
After doing some research I've become pretty convinced that this is an issue with how the LINQ provider is handling the query. This issue was reported to the developer a long while ago and marked as "Won't Fix" because the developer was not familiar with VB.
I'd really appreciate any assistance in either providing the developer relevant details to fix the issue on their side, or more realistically help pointing out what I'm doing wrong.
The Api for Jira can get a little complex and it is easy to end up using an older one that won't do what you want. I have had the best results using the nuget package "Atlassian.SDK".
With this it is relatively trivial to do what you want. this is C# code but I guess the vb.net can't be much different.
this.JiraConnection = Jira.CreateRestClient(jiraUrl, jiraUser, jiraPassword);
string jql = "project = TC";
IEnumerable<Issue> issues = this.JiraConnection.GetIssuesFromJql(jql, 100);
This allows you to use use the Jira Query Language to get the issues you want, which in my tests proved faster & more reliable than querying using Linq.
HTH
IT;S VB.NET not C# and i don't know how to exchange source from C# to VB.NET, THANKS
I want to get all paths from all running processes.
This is my source so far:
For Each p As Process In Process.GetProcesses
Try
ListBox1.Items.Add(p.MainModule.FileName.ToString)
Catch ex As Exception
RichTextBox1.Text = ex.Message
End Try
Next
But I can't get all path folders of the running processes.
If I examine the ex.Message, the response is like this
Unable to enumerate the process modules.
But if I not using ex.Message, the response is like this :
System.ComponentModel.Win32Exception was unhandled
ErrorCode=-2147467259
HResult=-2147467259
Message=A 32 bit processes cannot access modules of a 64 bit process.
NativeErrorCode=299
Source=System
StackTrace:
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at Anti_Cheat.Form1.Button6_Click(Object sender, EventArgs e) in c:\users\adiyatma\documents\visual studio 2012\Projects\Anti Cheat\Anti Cheat\Form1.vb:line 40
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 System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Anti_Cheat.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Can anyone help me?
Take a look at the so-question How to get the full path of running process?
Instead of
ListBox1.Items.Add(p.MainModule.FileName.ToString)
try
ListBox1.Items.Add(p.Modules(0).FileName.ToString)
Edit:
Have you tried to evaluate the property of different processes directly? Maybe there is a certain process you cannot access, resulting in the error described.
You can try to iterate trough the processes one by one by creating the following loop:
For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
Try
Console.WriteLine(p.Modules(0).FileName)
Catch ex As Exception
Console.WriteLine(String.Format("{0}: Error - {1}", p.ProcessName, ex.Message))
End Try
Next
By doing so, you should be able to determine the processes you're not allowed to access and get a couple of processes you should be able to experiment with.
Mate the problem is obvious, you are targeting 32-bit but are testing your app on a computer that has 64-bit installed system, that's why you get the error.
Message=A 32 bit processes cannot access modules of a 64 bit process.
to fix this issue you should make the target 64-bit, there IS NO SOLUTION for this...
if you know the famous procexp (sysinternals) it has two separate application, when the system is 32-bit it lauches an instance of 32-bit, but when it is 64-bit it lauches another separate process for the system...
so if you want to deal with this issue, you have to make two instances for system compability,
Hope this is helpful
ive been working on a screenshot program that upload the image automaticly to an FTP server. but i get an error when i try to use:
PictureBox1.Image.Save("C:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
and i always get the Error:
A generic error occurred in the GDI+
ErrorCode: -2147467259
source- System.Drawing
Full ERROR CODE BELOW (vid = at in english):
System.Runtime.InteropServices.ExternalException was unhandled
ErrorCode=-2147467259
Message="Ett allmänt fel uppstod i GDI+."
Source="System.Drawing"
StackTrace:
vid System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
vid mostlyTesting.Form1.Timer1_Tick(Object sender, EventArgs e) i C:\Users\emil\AppData\Local\Temporary Projects\mostlyTesting\Form1.vb:rad 22
vid System.Windows.Forms.Timer.OnTick(EventArgs e)
vid System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
vid System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
vid System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
vid System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
vid System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
vid System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
vid Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
vid Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
vid Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
vid mostlyTesting.My.MyApplication.Main(String[] Args) i 17d14f5c-a337-4978-8281-53493378c1071.vb:rad 81
vid System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
vid Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
vid System.Threading.ThreadHelper.ThreadStart()
InnerException:
I think what you want is this here
Me.PictureBox1.Image.Save(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFile.bmp"))
I hope it helps you. Good luck!
Update:
After I saw Plutonix comment, I tried what he said and It's true, When I try to save the image on C:\ (root folder) It gave me the same error as its giving to you. When I tried with a different path, it worked! The only way is to Run as Administrator and It works for C:\
I'm working on a directory Project where in when the program is not being use or idle for 3 minutes, a form that act as stand-by screen that plays a video will show (I use AxWindowsMediaPlayer). By clicking anywhere in the form, the stand-by screen will close and will be back to the main form.
The program works fine but SOMETIMES during the closing of stand-by screen form, the application crashes with the error:
AccessViolationException was unhandled. Attempted to read or write
protected memory. This is often an indication that other memory is
corrupt.
and this is the complete error detail:
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Windows.Forms
StackTrace:
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 System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at BuildingDirectory.main.tmrTime_Tick(Object sender, EventArgs e) in C:\Users\pc\Documents\Visual Studio 2010\Projects\testing\BuildingDirectory\BuildingDirectory\main.vb:line 128
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.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 System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at BuildingDirectory.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The weird thing is, the line of code where the error is pointing, is enclosed in try catch block specifically catching AccessViolationException(it was exception before but still encountered the problem):
Private Sub tmrTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTime.Tick
Try
lblTime.Text = TimeOfDay
If standby = 2 Then
standby += 1
standByScreen.ShowDialog() ---> this is where the error is pointing to
Else
standby += 1
End If
Catch ex As AccessViolationException
MessageBox.Show(ex.ToString)
End Try
End Sub
Please help me, thank you
This might be due to early binding of the standByScreen variable. Note that the error is caused due to the System.Windows.Forms class
Try:
Dim stndByScreen As Object
stndByScreen = standByScreen
stndByScreen.ShowDialog()
instead of directly using standByScreen
I have a form where the user can first scan to a bitmap. When scan is done, and the bitmap is loaded, I have 4 text boxes that are then enabled. Next to each text box, I have a button called "Cut from image". When the user clicks the button, they can click and drag in the bitmap to get the selected text using MODI.
This works perfect except for one annoying bug: When I click a "Cut from image" button and drag a square, it gets the information nicely to the text box. Then, if i click to the next text box, it goes very well, but if I use the tab key to leave the field, I get a "Parameter is not valid" ArgumentException and it does not show any help for where in the code the crash is made. I can tab around in the form with no problems at all, but once the bitmap is scanned, it crashes like 9 out of 10 times when I use the tab key.
I tried to override the tab key (just for debugging) using this:
Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
MsgBox("TAB is currently disabled!")
Return False 'Tried True as well, just in case
End Function
...but it still crashes.
Any suggestions about what's wrong? Since I don't know where to begin debugging I can't tell what code to show.
EDIT 1
Here is the stack trace for the ArgumentException that gets thrown:
at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.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 ORC_Testing.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
EDIT 2
Here is how I'm scanning/loading the image:
Dim filename As Collection
filename = TwainHandler.ScanImages("c:\scan\", "tif")
Dim ScannedFile As Image = Image.FromFile(filename(1))
PictureBox1.Image = ScannedFile
PictureBox1.Width = ScannedFile.Width
' etc.
Your problem is likely that, at some point, you are calling the Dispose method on one of your Image objects. When you call Image.Dispose, it deletes the underlying image data from memory, so the Image object still exists, but is invalid because it no longer contains an actual image. When you set the PictureBox.Image property to a loaded Image object, the PictureBox control assumes that the Image object will remain valid so that it can reuse it later any time the control needs to repaint itself to the screen. For instance:
Dim myImage As Image = Image.FromFile("file path")
PictureBox1.Image = myImage
PictureBox1.Refresh() ' This works
myImage.Dispose()
PictureBox1.Refresh() ' This throws an exception because it tries to access the disposed Image object
The PictureBox control will automatically dispose of the image for you when it is disposed, so you don't need to worry about disposing it yourself. The only time you should be disposing your images is when you are not giving them to any other objects for later use.
Here is my solution, somebody could use it, even though the question is old.
Dim myImage As Image = Image.FromFile("file path")
PictureBox1.Image = myImage.clone // Use clone to give a new copy not a reference of image
PictureBox1.Refresh() // This works
myImage.Dispose()
PictureBox1.Refresh() // This works also because we've a copy not reference
PictureBox1.Image = myImage.Clone
This way you are using a copy of the image so it does not matter what happens with the original