1 Attachment(s)
[3.0/LINQ] Weird ArgumentException
I'm getting an unusual ArgumentException and I don't know how to fix it.
Message: Parameter is not valid.
Source: "System.Drawing"
TargetSite: {Int32 get_Width()}
StackTrace: at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at Telerik.WinControls.Paint.RadGdiGraphics.DrawBitmap(Image image, Int32 x, Int32 y, Double opacity)
at Telerik.WinControls.Paint.RadGdiGraphics.DrawBitmap(Image image, Int32 x, Int32 y)
at Telerik.WinControls.Primitives.ReflectionPrimitive.PaintElement(IGraphics graphics, Single angle, SizeF scale)
at Telerik.WinControls.RadElement.PaintElement(Graphics rawGraphics, IGraphics graphics, Single angle, SizeF scale, Region currentClip)
at Telerik.WinControls.RadElement.Paint(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.PaintOverride(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.VisualElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.Paint(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.PaintOverride(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.VisualElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.Paint(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadItem.PaintOverride(IGraphics screenRadGraphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.VisualElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.Paint(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadItem.PaintOverride(IGraphics screenRadGraphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.VisualElement.PaintChildren(IGraphics graphics, Rectangle clipRectange, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadElement.Paint(IGraphics graphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RadItem.PaintOverride(IGraphics screenRadGraphics, Rectangle clipRectangle, Single angle, SizeF scale, Boolean useRelativeTransformation)
at Telerik.WinControls.RootRadElement.Paint(IGraphics graphics, Rectangle clipRectangle)
at Telerik.WinControls.RadControl.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at Telerik.WinControls.RadControl.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.IMsoCompo nentManager.FPushMessageLoop(Int32 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 TrailBlazer.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly 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)
at System.Threading.ThreadHelper.ThreadStart()
The familiar exception window appears when executing the code, however no line is highlighted.
The code which is executing at the time of the exception is the following:
Code:
private void radCarousel1_ItemDataBound(object sender, ItemDataBoundEventArgs e)
{
RadButtonElement button = (RadButtonElement)e.DataBoundItem;
string str = (string)e.DataItem;
int tmp = str.LastIndexOf('\\') + 1;
button.Tag = str;
button.Text = str.Substring(tmp, str.Length - tmp);
//button.AutoSize = false;
button.DisplayStyle = DisplayStyle.Image;
//button.TextAlignment = ContentAlignment.BottomCenter;
}
private void radCarousel1_SelectedIndexChanged(object sender, EventArgs e)
{
int visibleItemCount = Math.Min(this.radCarousel1.VisibleItemCount, this.radCarousel1.Items.Count);
int centerOffset = visibleItemCount / 2;
int itemIndex;
int selectedIndex = this.radCarousel1.SelectedIndex;
RadItemCollection carouselItems = this.radCarousel1.Items;
if (selectedIndex >= 0)
{
//Debug.Write("Loading items: ");
for (int i = selectedIndex - centerOffset;
i - (selectedIndex - centerOffset) < visibleItemCount;
i++)
{
itemIndex = GetActualItemIndex(i);
RadButtonElement button = (RadButtonElement)carouselItems[itemIndex];
if (button.Image == null && !string.IsNullOrEmpty(button.Text))
{
Image res = Image.FromFile((string)button.Tag);
//int tmp = (int)(129d * ((double)res.Height / res.Width));
button.Image = res.GetThumbnailImage(129, (int)(129d * ((double)res.Height / res.Width)), this.GetThumbnailImageAbort, IntPtr.Zero);
imagesLoaded++;
//Debug.Write(itemIndex + ", ");
}
}
//Debug.WriteLine("");
}
if (previousSelectedIndex >= 0)
{
//Debug.Write("Unloading images: ");
for (int i = previousSelectedIndex - centerOffset;
i - (previousSelectedIndex - centerOffset) < visibleItemCount;
i++)
{
itemIndex = GetActualItemIndex(i);
int firstVisibleItemIndex = GetActualItemIndex(selectedIndex - centerOffset);
if (firstVisibleItemIndex + visibleItemCount < carouselItems.Count)
{
if (itemIndex >= firstVisibleItemIndex &&
itemIndex < firstVisibleItemIndex + visibleItemCount)
continue;
}
else
{
if (itemIndex >= firstVisibleItemIndex ||
itemIndex < GetActualItemIndex(firstVisibleItemIndex + visibleItemCount))
continue;
}
RadButtonElement button = (RadButtonElement)carouselItems[itemIndex];
if (button.Image != null)
{
button.Image = null;
imagesLoaded--;
//Debug.Write(itemIndex + ", ");
}
}
//Debug.WriteLine("");
}
previousSelectedIndex = selectedIndex;
//Debug.WriteLine(imagesLoaded);
}
Since the line in red is the only place where System.Drawing is involved, I am inclined to believe that the Telerik WinForms package I'm using is the source of the exception, however I was unable to determine the exact cause. The weird thing is that the same code in Telerik's own example works fine - see attachment. Can anyone please help?
Re: [3.0/LINQ] Weird ArgumentException
Quote:
TargetSite: {Int32 get_Width()}
StackTrace: at System.Drawing.Image.get_Width()
Are you passing a negative/invalid width?
Re: [3.0/LINQ] Weird ArgumentException
I'm not passing anything directly as you can see from the code above. Actually, since it's get_Width and not set_Width, I think a value is being retrieved...
Re: [3.0/LINQ] Weird ArgumentException
What parameter does the ArgumentException indicate is the error? It normally provides this information.
Put a breakpoint on the line you suspect to be causing the exception and see if it is or not.
If the app falls over before this point, then it is not the line you suspect. If it does not, check all the values that are present and see if they are what you anticipate they should be and then step through the code.
i.e. Does the button.Tag property have the correct string file path for your images that you expect it to be? What are the values of res.Width and res.Height, etc.
If the error should lie within these 'Telerik' controls, then I would guess you will have no option to contact them.
Re: [3.0/LINQ] Weird ArgumentException
Unfortunately the ArgumentException doesn't provide that info, the ParamName field shows null, that was the first thing I tried to find out.
I just got an idea, I'll get back to you in a bit.
Re: [3.0/LINQ] Weird ArgumentException
This article gave me an idea: http://msmvps.com/blogs/bill/archive.../04/89234.aspx
I know it's weird but replacing String.IsNullOrEmpty with manual null and zero-length checking really did help. Now the exception only appears when the items are cycled through too quickly. I believe that although String.IsNullOrEmpty was not the direct cause of the exception, it did slow the code down. It may not sound logical at this stage but pls hear me out.
The lack of a highlighted line, together with the peculiar two-buttoned exception window point towards a concurrency-related exception, or at least an exception thrown on a different thread. The carousel is made so that the items which are not shown are not kept in memory, however the items which should enter the control next are stored in memory. So if you cycle through the items quickly enough, not giving the underlying code enough time to catch up, the application will try to dispose of items which have not been created in memory yet. Here comes the issue with the IsNullOrEmpty. My guess is that it would slow the code down (over repeated use, of course) enough to cause this problem.
Anyway, that's my theory and since the control is up and running without problems in reasonable conditions, I'll leave it as it is. Thank you all for your time and effort. Happy hollidays!
Re: [3.0/LINQ] Weird ArgumentException
Well I must say that sounds far from satisfactory IMO. I would keep a close eye on that if I were you.
The construct of String.IsNullOrEmpty looks perfectly acceptable to me:
From .NET Reflector:
Code:
public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}
Quote:
So if you cycle through the items quickly enough, not giving the underlying code enough time to catch up, the application will try to dispose of items which have not been created in memory yet.
Do you create the 'items' yourself? If so, why should your app try to dispose of items that have not been created? At the very least you should be able to check if they are null before disposing of them.
Re: [3.0/LINQ] Weird ArgumentException
I'm not creating the items, I'm binding a list of filenames to the DataSource property.
Weird thing is, it was working just fine until I rebuilt the project and now it crashes just the same as it did :(
The only thing I can think of ATM is rewriting the code instead of using the working example code provided by Telerik. I would have considered this much earlier but I'm not too proficient at writing C# code.
Re: [3.0/LINQ] Weird ArgumentException
Have you tried a breakpoint yet?
As it seems like the error occurs only under certain circumstances, put a try..catch around the line you suspect to be causing problems, and set a breakpoint inside the catch block:
csharp Code:
try
{
button.Image = res.GetThumbnailImage(129, (int)(129d * ((double)res.Height / res.Width)), this.GetThumbnailImageAbort, IntPtr.Zero);
}
catch (ArgumentException ex)
{
MessageBox.Show("Crash"); // Make a breakpoint on this line.
}
When the error happens, you will be able to see the value of the local variables, and determine if any are suspect.
Might give you a clue, and might be useful info to give to Telerik if you need to.