public void Move(PictureBox picture, int dx, int dy, int Delay) { int x = picture.Location.X, y = picture.Location.Y; while (x != dx || y != dy) { // mre.WaitOne(); if (x < dx) { x += 2; } if (x > dx) { x -= 2; } if (y < dy) { y += 2; } if (y > dy) { y -= 2; } try { picture.BeginInvoke((Action)(() => picture.Location = new Point(x, y))); if (Delay != 0) { picture.Invoke((Action)(() => picture.Refresh())); Thread.Sleep(Delay); } } catch (Exception) { } } }
public void MoveC(PictureBox picture, int dx, int dy, int Delay) { int x = picture.Location.X, y = picture.Location.Y; while (x != dx || y != dy) { if (Delay != 0) { if (y != dy) { if (y < dy) { y += 2; } if (y > dy) { y -= 2; } } else { if (x < dx) { x += 2; } if (x > dx) { x -= 2; } } try { picture.BeginInvoke((Action)(() => picture.Location = new Point(x, y))); } catch (Exception) { } Thread.Sleep(Delay); } else { x = dx; y = dy; picture.BeginInvoke((Action)(() => picture.Location = new Point(dx, dy))); } } }
private void setNewFrame(PictureBox canvas, Bitmap frame) { // Invoke is required when updating a control on the GUI thread. if (canvas.InvokeRequired) { canvas.BeginInvoke(new MethodInvoker(delegate() { setFrame(canvas, frame); })); } else { setFrame(canvas, frame); } }
private void updater(PictureBox pb, Data d) { if (pb.InvokeRequired) { pb.BeginInvoke(new SetImageCallback(updater), pb, d); } else { pb.Image = d.Img; lbProgress.Text = (!d.Progress.Equals("Done!")) ? d.Progress + "/" + 100 : d.Progress; lbPack.Text = d.Pack.X + "-" + d.Pack.Y; lbDirSize.Text = (size / 1024) + "KB" + " (" + size + ")"; btnStart.Enabled = d.BtnEnable; btnStart.Text = d.BtnText; } }
private static void RunLoading(string url, string path, PictureBox picture, TextBox box = null) { new Thread(() => { Timer loadingTimer = null; if (!BstPicLoader.LoadingTimers.ContainsKey(picture)) { // 更新图片成读取状态,如果Dictionary里已经有这个PictureBox的Timer了,说明loading图已经加载了 var loadingGif = new BstGifImage(BstManager.PathLoadingGif) { ReverseAtEnd = false }; loadingTimer = new Timer(50); loadingTimer.Elapsed += (s, e) => { MethodInvoker loadingAction = () => { picture.Image = loadingGif.GetNextFrame(); }; try { picture.BeginInvoke(loadingAction); } catch (InvalidOperationException ex) { BstLogger.Instance.Log(ex.ToString()); // 因为我们可能会在GUI_Picture的UI中的PictureBox里显示loading动态图 // 而上述的窗口可能在关闭后被销毁,这里我们需要处理窗口被销毁后的错误 // 这时候Timer应该在Dictionary里注册过了 if (BstPicLoader.LoadingTimers.ContainsKey(picture)) { var timer = BstPicLoader.LoadingTimers[picture]; timer.Enabled = false; BstPicLoader.LoadingTimers.Remove(picture); timer.Dispose(); } } }; BstPicLoader.LoadingTimers.Add(picture, loadingTimer); loadingTimer.Enabled = true; } else { loadingTimer = BstPicLoader.LoadingTimers[picture]; } // 检查是否有本地缓存 byte[] blob = null; if (File.Exists(path)) { // 本地缓存存在,直接读取 blob = BstManager.GetBytesFromFile(path); } else { // 下载图片 blob = BstManager.DownloadImageFile(url, path); if (blob == null) { // 图片下载失败 BstManager.ShowMsgInTextBox(box, string.Format(BstI18NLoader.Instance.LoadI18NValue("BstPicLoader", "picDownloadFailed"), url)); // 停止动态图更新 loadingTimer.Enabled = false; BstPicLoader.LoadingTimers.Remove(picture); loadingTimer.Dispose(); // 更新下载失败icon var errorIconBitmap = BstManager.ConvertByteToImage(BstManager.Instance.ErrorIconBytes); MethodInvoker updateErrorAction = () => picture.Image = errorIconBitmap; try { picture.BeginInvoke(updateErrorAction); } catch (Exception ex) { BstLogger.Instance.Log(ex.ToString()); } return; } } loadingTimer.Enabled = false; // 加载完成,停止动态loading图的更新 BstPicLoader.LoadingTimers.Remove(picture); // 加载完成,删除Dictionary里注册的Timer loadingTimer.Dispose(); BstManager.ShowMsgInTextBox(box, string.Format(BstI18NLoader.Instance.LoadI18NValue("BstPicLoader", "picDownloadSucceed"), url)); // 转换成位图 var bitmap = BstManager.ConvertByteToImage(blob); // 更新图片内容 MethodInvoker updateAction = () => picture.Image = bitmap; try { picture.BeginInvoke(updateAction); } catch (Exception ex) { BstLogger.Instance.Log(ex.ToString()); } }).Start(); }