参数c#无效
本文关键字:无效 参数 | 更新日期: 2023-09-27 18:27:06
我正在活动报告中加载图像。我经历了许多问题,终于找到了这个解决方案。但当我尝试运行代码时,我得到了"参数无效"的异常。
代码:
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;
}
}
异常:
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:
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();
}
实际的图像是一个较小的图像,我需要在全尺寸视图中显示。
请帮忙。
我认为您可以简化CreateIndexedImage
方法:
public static Image CreateIndexedImage(string path)
{
return Image.FromFile(path);
}
如果您需要创建图像对象的副本,您可能需要使用Image.Clone
:
public static Image CreateIndexedImage(string path)
{
using (var sourceImage = (Bitmap)Image.FromFile(path))
{
var targetImage = sourceImage.Clone;
// manipulate image
...
return targetImage;
}
}