winforms - transition between images for picturebox in c# -
winforms - transition between images for picturebox in c# -
possible duplicate: transition of images in windows forms image box
i utilize code below alter images in picturebox every 5 seconds, it's not looking when changing image: want transition effect between images.
used codeprivate void backgroundworker1_dowork(object sender, doworkeventargs e) { bitmap[] pictures = new bitmap[9]; pictures[0] = new bitmap(@"library images\cf3.jpg"); pictures[1] = new bitmap(@"library images\cf4.jpg"); pictures[2] = new bitmap(@"library images\l1.jpg"); pictures[3] = new bitmap(@"library images\l2.jpg"); pictures[4] = new bitmap(@"library images\l3.jpg"); pictures[5] = new bitmap(@"library images\l4.jpg"); pictures[6] = new bitmap(@"library images\l5.jpg"); pictures[7] = new bitmap(@"library images\l6.jpg"); pictures[8] = new bitmap(@"library images\l7.jpg"); random random = new random(); while (true) { int effort = random.next(0, pictures.length); picturebox1.image = pictures[attempt]; system.threading.thread.sleep(5000); } }
example code appreciated in advance...
simply take new codefile , paste below code in
answerusing system; using system.drawing; using system.drawing.imaging; using system.windows.forms; public class blendpanel : panel { private image mimg1; private image mimg2; private float mblend; public blendpanel() { setstyle(controlstyles.allpaintinginwmpaint | controlstyles.userpaint | controlstyles.optimizeddoublebuffer, true); } public image image1 { { homecoming mimg1; } set { mimg1 = value; invalidate(); } } public image image2 { { homecoming mimg2; } set { mimg2 = value; invalidate(); } } public float blend { { homecoming mblend; } set { mblend = value; invalidate(); } } protected override void onpaint(painteventargs e) { if (mimg1 == null || mimg2 == null) e.graphics.fillrectangle(new solidbrush(this.backcolor), new rectangle(0, 0, this.width, this.height)); else { rectangle rc = new rectangle(0, 0, this.width, this.height); colormatrix cm = new colormatrix(); imageattributes ia = new imageattributes(); cm.matrix33 = mblend; ia.setcolormatrix(cm); e.graphics.drawimage(mimg2, rc, 0, 0, mimg2.width, mimg2.height, graphicsunit.pixel, ia); cm.matrix33 = 1f - mblend; ia.setcolormatrix(cm); e.graphics.drawimage(mimg1, rc, 0, 0, mimg1.width, mimg1.height, graphicsunit.pixel, ia); } base.onpaint(e); } }
build project. can drop blendpanel top of toolbox onto form. here's sample programme uses it:
private float mblend; private int mdir = 1; public int count = 0; public bitmap[] pictures; public void myphoto() { pictures = new bitmap[9]; pictures[0] = new bitmap(@"library images\cf3.jpg"); pictures[1] = new bitmap(@"library images\cf4.jpg"); pictures[2] = new bitmap(@"library images\l1.jpg"); pictures[3] = new bitmap(@"library images\l2.jpg"); pictures[4] = new bitmap(@"library images\l3.jpg"); pictures[5] = new bitmap(@"library images\l4.jpg"); pictures[6] = new bitmap(@"library images\l5.jpg"); pictures[7] = new bitmap(@"library images\l6.jpg"); pictures[8] = new bitmap(@"library images\l7.jpg"); timer1.interval = 50; //time of transition timer1.tick += blendtick; seek { blendpanel1.image1 = pictures[count]; blendpanel1.image2 = pictures[++count]; } grab { } timer1.enabled = true; } private void blendtick(object sender, eventargs e) { mblend += mdir * 0.02f; if (mblend > 1) { mblend = 0.0f; if ((count + 1) < pictures.length) { blendpanel1.image1 = pictures[count]; blendpanel1.image2 = pictures[++count]; } else { blendpanel1.image1 = pictures[count]; blendpanel1.image2 = pictures[0]; count = 0; } } blendpanel1.blend = mblend; }
you'll need modify new bitmap(@"yourimagepath");
calls. build , run. should see displayed image smoothly morph first image sec image without flickering.
i hope helps other...
c# winforms
Comments
Post a Comment