简体   繁体   English

参数无效C#

[英]Parameter not valid c#

I am loading image in my Active Report. 我正在我的活动报告中加载图像。 I went through many issues and at last I found this solution. 我经历了很多问题,终于找到了解决方案。 But when I tried to run the code I am getting "Parameter not valid" exception. 但是,当我尝试运行代码时,出现“参数无效”异常。

Code: 码:

  try
     {

 if (File.Exists(this.txtProtoImage.Value.ToString()))
            {
                this.imgProtoImage.Image = CreateIndexedImage(this.txtProtoImage.Value.ToString());
              //  this.imgProtoImage.Image = System.Drawing.Image.FromFile(this.txtProtoImage.Value.ToString());
                this.imgProtoImage.SizeMode = GrapeCity.ActiveReports.SectionReportModel.SizeModes.Zoom;
            }

        }
        catch (Exception ex)
        {
            throw ; // Line 119
        }
        finally
        {
            GC.Collect();
        }

[DllImport("Kernel32.dll", EntryPoint = "CopyMemory")] 
private extern static void CopyMemory(IntPtr dest, IntPtr src, uint length); 

public static Image CreateIndexedImage(string path) { 


using (var sourceImage = (Bitmap)Image.FromFile(path)) { 
    var targetImage = new Bitmap(sourceImage.Width, sourceImage.Height, 
      sourceImage.PixelFormat); 
    var sourceData = sourceImage.LockBits(
      new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 
      ImageLockMode.ReadOnly, sourceImage.PixelFormat); 
    var targetData = targetImage.LockBits(
      new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 
      ImageLockMode.WriteOnly, targetImage.PixelFormat); 
    CopyMemory(targetData.Scan0, sourceData.Scan0, 
      (uint)sourceData.Stride * (uint)sourceData.Height); 
    sourceImage.UnlockBits(sourceData); 
    targetImage.UnlockBits(targetData); 
    targetImage.Palette = sourceImage.Palette; ///Exception here
    return targetImage; 
  } 
} 

Exception: 例外:

 System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Parameter is not valid.
      Source=Series.Presentation
      StackTrace:
   at Series.Presentation.Reports.RevReport.Ma_Format(Object sender, EventArgs e) in e:\UserNew\NewApp\Presentation\Reports\RevReport.cs:line 119
   at GrapeCity.ActiveReports.SectionReportModel.Section.#7Ab()
   at GrapeCity.ActiveReports.SectionReportModel.Section.#HBb(SectionReport report, PointF location)
   at #sxA.#vqb.#dzb(Section section)
   at #sxA.#vqb.#vEb()
   at #sxA.#vqb.#bZA(Page newPage, Single left, Single top, Single right, Single bottom, UInt32 flags, UInt32& status)
   at GrapeCity.ActiveReports.SectionReport.#4yb()
   at GrapeCity.ActiveReports.SectionReport.Run(Boolean syncDocument)
   at GrapeCity.ActiveReports.SectionReport.Run()
   at Series.Presentation.View.RevReportViewerWindow.OpenRevReport() in e:\UserNew\NewApp\Presentation\View\RevReportViewerWindow.xaml.cs:line 164
   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: 

RevReportViewerWindow.xaml.cs: RevReportViewerWindow.xaml.cs:

 public void OpenRevReport()
        {
 Reports.RevReport MaReport = null;
            try
            {
                MaReport = new Reports.RevReport(lstUsers, blnFlag);

                MaReport.Run();

                App.Current.Dispatcher.Invoke(() =>
                {
                    ReportViewer.Document = MaReport.Document;

                    RevHost.Child = ReportViewer;
                    ReportViewer.ViewType = GrapeCity.Viewer.Common.Model.ViewType.Continuous;
                    ReportViewer.Visible = true;

                    IsBusy = false;
                    BusyContent = "Ready.";
                });

            }
            catch (Exception ex)
            {

                throw ex;  //164th line where the exception is thrown.
            }
            finally
            {
                GC.Collect();
            }

The actual image is a smaller one and I need to show in a full size view. 实际图像较小,我需要以完整尺寸显示。

Kindly help. 请帮助。

I think you could simplify your CreateIndexedImage method: 我认为您可以简化CreateIndexedImage方法:

public static Image CreateIndexedImage(string path) 
{ 
    return Image.FromFile(path);
}

If you need to create a copy of your image object, you might want to use Image.Clone : 如果需要创建图像对象的副本,则可能要使用Image.Clone

public static Image CreateIndexedImage(string path) 
{ 
    using (var sourceImage = (Bitmap)Image.FromFile(path)) 
    { 
        var targetImage = sourceImage.Clone; 

        // manipulate image 
        ...

        return targetImage;
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM