c# - Refreshing PictureBox causes ArgumentException Parameter is not valid -
i can't life of me figure out. i'm modifying bitmap picturebox points to. someone drags image across picturebox, , image updated looks dragging across picturebox. happens on refreshing picturebox, argumentexception parameter not valid error on application.run line, took me while figure out causing because there no more information in exception.
i've created class derives picturebox , overrode onpaint method. has method, refreshimage, invoke , refresh itself
if not refresh picturebox, not error, picturebox image never updated
i have main loop running in updates logic. logic when mouse dragged, image offset updated.
after loop completes logic, attempts refresh picturebox. ive found when comment out refresh line, not have problem, picturebox never updated. i've added method custom picturebox class called updateimage invokes refreshes.
the overridden onpaint method takes logic custom loop thread, , updates bitmap based on offset end loop thread updated
the main form created on main thread, , picturebox created on main thread, i've check that
invoking working should, backend thread invokes main thread when refreshing picturebox
commenting out refresh line makes error go away, can't see picturebox update
so there suggestions on either better way or solution error?
here part of derrived picturebox class
public class pictureboxex : picturebox { public int m_xoffset = 0; private bitmap m_picture; public pictureboxex() { m_picture = new bitmap(690, 600); this.image = m_picture; } protected override void onpaint(painteventargs pe) { base.onpaint(pe); drawbitmaps(); // updates m_picture bitmap in class graphics g = pe.graphics; g.drawimage(this.m_picture, new point(0, 0)); } public void refreshimage() { if(this.invokerequired) { methodinvoker d = new methodinvoker(refreshimage); this.invoke(d); } else { this.refresh(); } } }
heres idea of main loop
private mainform m_mainwindow; // derrives form private void mainthread() { int currtime = system.environment.tickcount; int lasttime = currtime; while (running) { currtime = system.environment.tickcount; m_mainwindow.update(currtime, lasttime); lasttime = currtime; thread.sleep(10); } }
heres update method kind of looks in mainwindow
pictureboxex m_leftpicturebox; public void update(int currtime, int lasttime) { // logic (all logic working primitive types, nothing actual bitmaps this.m_leftpicturebox.refreshimage(); // if comment out, not exception, not update picturebox } }
so 1 more thing, why doesn't picturebox refresh when main form refreshes? or main form not refreshing? i'm sure if case see cursor making not being cleared. put breakpoint in onpaint method of custom picturebox class, called once when first created, not after that, have manually refresh it.
edit: not sure more code give solve this, if call function background thread (this function in form class made), same exception immediately. exception happens on this.refresh() , on main thread (so after invoked). though in try-catch block, exception still says uncaught , closes app
public void invokerefresh() { if (this.invokerequired) { refreshdelegate d = new refreshdelegate(invokerefresh); this.invoke(d, null); } else { try { this.refresh(); } catch { } } }
Comments
Post a Comment