delegate void RenderSection(int top, int bottom);
/// <summary> /// This is the delegate that renders a part of the image, a delegate for RenderSection /// </summary> public void RenderASection(int top, int bottom) { // Raytrace just this section of the scene Bitmap SectionBitmap = new Bitmap(width, height); RayTracer rayTracer = new RayTracer(width, height, (int x, int y, System.Drawing.Color color) => { SectionBitmap.SetPixel(x, y, color); } ); rayTracer.Render(rayTracer.DefaultScene, top, bottom); // Copy my part of the scene into the overall scene lock (bitmap) { Graphics g = Graphics.FromImage(bitmap); g.DrawImage( SectionBitmap, new Rectangle(0, top, width, bottom), new Rectangle(0, top, width, bottom), GraphicsUnit.Pixel); } }
public void Run() { width = 800; height = 800; bitmap = new Bitmap(width, height); RenderSection myDelegate = RenderASection; // Run it with different parameters IAsyncResult ar1 = myDelegate.BeginInvoke(0, 200, CallbackMethod, null); IAsyncResult ar2 = myDelegate.BeginInvoke(200, 400, CallbackMethod, null); IAsyncResult ar3 = myDelegate.BeginInvoke(400, 600, delegate(IAsyncResult ar) { Console.WriteLine("Section finished (anonymous delegate)"); }, null); IAsyncResult ar4 = myDelegate.BeginInvoke(600, height, CallbackMethod, null); // Wait for them to finish myDelegate.EndInvoke(ar1); myDelegate.EndInvoke(ar2); myDelegate.EndInvoke(ar3); myDelegate.EndInvoke(ar4); // Save the output bitmap bitmap.Save("RayTrace.png"); } /// <summary> /// This is a function we can use as a delegate for AsyncCallback /// </summary> void CallbackMethod(IAsyncResult ar) { Console.WriteLine("Section finished"); }
Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.
(Comp Sci, Hons - UTS)
Software Developer (Freelancer / Contractor) in Australia.
I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!
Get in touch:
[email protected]
github.com/chrishulbert
linkedin