Image 导致“参数无效”的原因。从资源设置图像时出现异常

Image 导致“参数无效”的原因。从资源设置图像时出现异常,image,embedded-resource,system.drawing,argumentexception,Image,Embedded Resource,System.drawing,Argumentexception,这是一个用户刚刚报告的。程序的这一部分已经在野外进行了广泛的测试和使用,并且在一段时间内没有改变。这似乎对她有用,只是最近才开始失败。由于映像来自资源,因此不需要对其进行处理。在我的测试中,通过检查代码,这应该总是发生在UI线程上,因此我最好的猜测是程序不知何故已损坏,导致资源中的图像实际上无效。这有意义吗?或者有更合理的解释吗。以下是调用堆栈: Msg: Parameter is not valid. Source: System.Drawing Assembly: System.Drawin

这是一个用户刚刚报告的。程序的这一部分已经在野外进行了广泛的测试和使用,并且在一段时间内没有改变。这似乎对她有用,只是最近才开始失败。由于映像来自资源,因此不需要对其进行处理。在我的测试中,通过检查代码,这应该总是发生在UI线程上,因此我最好的猜测是程序不知何故已损坏,导致资源中的图像实际上无效。这有意义吗?或者有更合理的解释吗。以下是调用堆栈:

Msg: Parameter is not valid.
Source: System.Drawing
Assembly: System.Drawing, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a
Stack: at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.GetPreferredSizeCore(Size
proposedSize)
at System.Windows.Forms.Control.GetPreferredSize(Size proposedSize)
at
System.Windows.Forms.Layout.TableLayout.GetElementSize(IArrangedElement
element, Size proposedConstraints)
at System.Windows.Forms.Layout.TableLayout.InflateColumns(ContainerInfo
containerInfo, Size proposedConstraints, Boolean measureOnly)
at System.Windows.Forms.Layout.TableLayout.ApplyStyles(ContainerInfo
containerInfo, Size proposedConstraints, Boolean measureOnly)
at System.Windows.Forms.Layout.TableLayout.LayoutCore(IArrangedElement
container, LayoutEventArgs args)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container,
LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs
levent)
at System.Windows.Forms.TableLayoutPanel.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at
System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement
affectedElement, String affectedProperty)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at
System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement
affectedElement, String affectedProperty)
at System.Windows.Forms.PictureBox.InstallNewImage(Image value,
ImageInstallationType installationType)
at System.Windows.Forms.PictureBox.set_Image(Image value)
at SayMore.Transcription.UI.OralAnnotationRecorderBaseDlg.UpdateDisplay()
at
SayMore.Transcription.UI.OralAnnotationRecorderBaseDlg.BeginRecording(TimeRange
timeRangeOfSourceBeingAnnotated)
at
SayMore.Transcription.UI.OralAnnotationRecorderBaseDlg.HandleRecordAnnotationMouseDown(Object
sender, MouseEventArgs e)
at
SayMore.Transcription.UI.OralAnnotationRecorderBaseDlg.OnLowLevelKeyDown(Keys
key)
at
SayMore.UI.LowLevelControls.MonitorKeyPressDlg.PreFilterMessage(Message& m)
at System.Windows.Forms.Application.ThreadContext.ProcessFilters(MSG&
msg, Boolean& modified)
at
System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at
System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(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 SayMore.Model.Files.ComponentFile.RecordAnnotations(Form frm,
AudioRecordingType annotationType)
at
SayMore.Transcription.UI.TextAnnotationEditor.<>c_DisplayClassb.<HandleRecordedAnnotationButtonClick>b_a()
at
SayMore.Transcription.UI.TextAnnotationEditor.ShowSegmentationDialog(Action
showDialog)
at
SayMore.Transcription.UI.TextAnnotationEditor.HandleRecordedAnnotationButtonClick(Object
sender, EventArgs e)
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.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.Callback(IntPtr hWnd, Int32 msg,

我不确定是哪个设置映像的调用导致了异常,但根据上下文,我怀疑是_labelRecordButton.Image设置为Resources.RecordingOralAnnotationInProgress的地方。关键是,在任何情况下,图像都只是来自资源,因此它应该始终有效。

可以是任何内容。众所周知,位图类会为任何类型的错误生成任何类型的异常,因此问题的原因不能从该异常类型派生。我猜可能是记忆不足

刚刚在“decoder=new JpegBitmapDecoderstream,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.OnLoad;”行中发现类似的错误参数无效包含该行的函数会针对不同的映像重复调用,并且在调用次数可变后会发生故障,但对于较大的映像,调用速度更快,这意味着内存问题。但是,使用任务管理器进行监视时,重复调用不会系统性地增加内存使用。已删除对JpegBitmapDecoder的调用,但在使用资源监视器检查的文件创建位图时,偶尔会出现相同的错误,在崩溃发生时,该文件在内存分配中未显示异常功能。当应用程序(如打开多个选项卡的浏览器)提交大量内存时,以及当应用程序提交更多内存时处理更大的图像时,崩溃更为频繁。所以它闻起来像是内存问题,但诊断不支持这一点。
    protected override void UpdateDisplay()
    {
        _recDeviceIndicator.UpdateDisplay();

        _labelListenButton.Image = (_waveControl.IsPlaying && _playingBackUsingHoldDownButton ?
            Resources.ListenToOriginalRecordingDown : Resources.ListenToOriginalRecording);

        _labelRecordButton.Image = (ViewModel.GetIsRecording() ?
            Resources.RecordingOralAnnotationInProgress : Resources.RecordOralAnnotation);

        _labelListenButton.Enabled = !ViewModel.GetIsRecording() &&
            (ViewModel.CurrentUnannotatedSegment != null || !ViewModel.GetIsFullyAnnotated());

        _labelRecordButton.Enabled = (ViewModel.GetSelectedSegmentIsLongEnough() &&
            _userHasListenedToSelectedSegment &&
            AudioUtils.GetCanRecordAudio(true) &&
            !_waveControl.IsPlaying && !ViewModel.GetIsAnnotationPlaying());

        _labelListenHint.Visible = _spaceBarMode == SpaceBarMode.Listen && _labelListenButton.Enabled;
        _labelRecordHint.Visible = _spaceBarMode == SpaceBarMode.Record && _labelRecordButton.Enabled && !_reRecording && _recordingErrorMessage == null;

        if (_spaceBarMode == SpaceBarMode.Done && _recordingErrorMessage == null)
        {
            if (!_labelFinishedHint.Visible)
            {
                _pictureIcon.Image = Resources.Green_check;
                _labelFinishedHint.Visible = true;
                _tableLayoutButtons.Controls.Add(_labelFinishedHint, 1, 0);
                _tableLayoutButtons.SetRowSpan(_labelFinishedHint, 3);
                AcceptButton = _buttonOK;
            }
        }
        else
        {
            UdateErrorMessageDisplay();

            if (_labelErrorInfo.Visible)
            {
                _pictureIcon.Image = Resources.Information_red;
                if (_labelFinishedHint.Visible)
                {
                    _labelFinishedHint.Visible = false;
                    _tableLayoutButtons.Controls.Remove(_labelFinishedHint);
                }
                _labelRecordHint.Visible = false;
            }
            else
            {
                _pictureIcon.Image = Resources.Information_blue;
            }

            float percentage = (_labelErrorInfo.Visible) ? 50 : 100;
            _tableLayoutButtons.RowStyles[0].Height = (_labelErrorInfo.Visible) ? percentage : 0;
            _tableLayoutButtons.RowStyles[1].Height = (_labelListenHint.Visible) ? percentage : 0;
            _tableLayoutButtons.RowStyles[2].Height = (_labelRecordHint.Visible) ? percentage : 0;
        }
        base.UpdateDisplay();
    }