这是一个使用 .NET 4.7.2 的 C# WinForms 应用程序。
我正在使用来自多页 TIFF 的自定义图片框(基本上是图片框和标签)填充 FlowPanel。我将 FlowPanel 中的图像数量限制为“页”,并在填充 FlowPanel 之前将 TIFF 中的单个图像转换为缩略图。这是使用后台工作程序填充流面板的代码:
private void DoFillImages()
{
Cursor.Current = Cursors.WaitCursor;
flowImages.Enabled = false;
flowImages.Controls.Clear();
SelectedImages.Clear();
BWImages.RunWorkerAsync();
}
private void BWImages_DoWork(object sender, DoWorkEventArgs e)
{
FillImageGrid();
}
private void FillImageGrid()
{
flowImages.Controls.Clear();
Image thumb = null;
int ctr = 0;
if (File.Exists(ImageFile))
{
string imageExt = Path.GetExtension(ImageFile);
switch (imageExt)
{
case ".JPG":
CurrentSelectedImageFileType = eSelectedImageFileType.JPG;
ImagesInRecord = 1;
AddImage(1, ImageFile);
break;
case ".TIF":
CurrentSelectedImageFileType = eSelectedImageFileType.TIF;
Image im = Image.FromFile(ImageFile);
FrameDimension dimension = FrameDimension.Page;
int ImagesInTIF = im.GetFrameCount(dimension);
if (ImagesInTIF % ImagesPerPage == 0)
PagesOfImages = ImagesInTIF / ImagesPerPage;
else
PagesOfImages = ImagesInTIF / ImagesPerPage + 1;
CurrentPage = 1;
for (int i = (CurrentPage - 1) * ImagesPerPage; i <= Math.Min(CurrentPage * ImagesPerPage, ImagesInTIF) - 1; i++)
{
string imagename = Path.GetFileNameWithoutExtension(ImageFile);
string imagefolder = Path.GetDirectoryName(ImageFile);
string thumbname = string.Format("{0}_{1}.thumb", imagename, i);
string thumbpath = Path.Combine(imagefolder, thumbname);
if (!File.Exists(thumbpath))
{
im.SelectActiveFrame(dimension, i);
Image img = new Bitmap(im);
thumb = img.GetThumbnailImage(80, 100, () => false, IntPtr.Zero);
thumb.Save(thumbpath, System.Drawing.Imaging.ImageFormat.Jpeg);
AddImage(i + 1, thumb, ImageFile, false);
img.Dispose();
}
else
{
thumb = Image.FromFile(thumbpath);
AddImage(i + 1, thumb, ImageFile, false);
}
}
thumb.Dispose();
im.Dispose();
functions.ClearMemory(); //Forces Garbage Collection
break;
}
}
}
public void AddImage(int PicNo, Image image, string ImageFile, bool IsMissing)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<int, Image, string, bool>(AddImage), PicNo, image, ImageFile, IsMissing);
}
else
{
int width = Constants.BaseImageWidth * SizeFactor;
int height = Constants.BaseImageHeight * SizeFactor;
CustomPicBox cpb = new CustomPicBox();
cpb.Name = string.Format("cpb{0}", PicNo);
cpb.PictureHeight = height;
cpb.Width = width;
cpb.Caption = string.Format("Image {0}", PicNo);
cpb.MouseDown += new MouseEventHandler(CustomPicBox_MouseDown);
cpb.QueryContinueDrag += new QueryContinueDragEventHandler(flowImages_QueryContinueDrag);
cpb.CaptionFont = new Font(cpb.Font, FontStyle.Bold);
cpb.ImageNumber = PicNo;
cpb.Picture = image;
cpb.IsMissing = IsMissing;
cpb.ImageFilename = ImageFile;
cpb.SizeMode = PictureBoxSizeMode.StretchImage;
flowImages.Controls.Add(cpb);
cpb = null;
}
}
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
private void BWImages_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
flowImages.Enabled = true;
Cursor.Current = Cursors.Default;
}
当我重新启用 FlowPanel 时,我收到“参数无效”错误,并带有以下堆栈跟踪:
at System.Drawing.Image.get_FrameDimensionsList() at System.Drawing.ImageAnimator.CanAnimate(Image image) at
System.Drawing.ImageAnimator.ImageInfo..ctor(Image image) at
System.Drawing.ImageAnimator.Animate(Image image, EventHandler
onFrameChangedHandler) at System.Windows.Forms.PictureBox.Animate(Boolean
animate) at System.Windows.Forms.PictureBox.Animate() at
System.Windows.Forms.PictureBox.OnEnabledChanged(EventArgs e) at
System.Windows.Forms.Control.OnParentEnabledChanged(EventArgs e) at
System.Windows.Forms.Control.OnEnabledChanged(EventArgs e) at
System.Windows.Forms.Control.OnParentEnabledChanged(EventArgs e) at
System.Windows.Forms.Control.OnEnabledChanged(EventArgs e) at
System.Windows.Forms.Control.set_Enabled(Boolean value) at
VersaNET.frmSearchDB.tmrDoneLoadingImages_Tick(Object sender, EventArgs e)
in d:\Visual Studio Projects\VersaNET\VersaNET\frmSearchDB.cs:line 3793 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.Unsa
feNativeMethods.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(Form
mainForm) at VersaNET.Program.Main() in d:\Visual Studio Projects\VersaNET
\VersaNET\Program.cs:line 52
为什么我会得到这个错误,为什么当我只是启用控件时?