Created
February 25, 2016 05:49
-
-
Save westonal/13b832aaef7e78da3fff to your computer and use it in GitHub Desktop.
Fast image merge profiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Drawing; | |
using System.Diagnostics; | |
using System.Drawing.Imaging; | |
namespace FastMergeImage | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Bitmap image1 = Image.FromFile(@"..\..\..\IMG_4522_hs.jpg") as Bitmap; | |
Bitmap image2 = Image.FromFile(@"..\..\..\IMG_4647_sm_blend.png") as Bitmap; | |
Time(GraphicsBlend, "Graphics blend", image1, image2); | |
Time(ByteBlend, "Byte blend", image1, image2); | |
} | |
public static void Time(Action<Bitmap, Bitmap, Bitmap> action, string name, Bitmap image1, Bitmap image2) | |
{ | |
using (Bitmap target = new Bitmap(image1.Width, image1.Height)) | |
{ | |
var sw = Stopwatch.StartNew(); | |
action(image1, image2, target); | |
sw.Stop(); | |
Console.WriteLine("{0} took {1}", name, sw.Elapsed); | |
target.Save(@"C:\temp\" + name + ".png", ImageFormat.Png); | |
} | |
} | |
public static void GraphicsBlend(Image image1, Image image2, Bitmap target) | |
{ | |
var g = Graphics.FromImage(target); | |
g.DrawImage(image1, 0, 0); | |
g.DrawImage(image2, 0, 0); | |
} | |
public static void ByteBlend(Bitmap image1, Bitmap image2, Bitmap target) | |
{ | |
var locked = target.LockBits(new Rectangle(0, 0, target.Width, target.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); | |
try | |
{ | |
var locked1 = image1.LockBits(new Rectangle(0, 0, target.Width, target.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); | |
try | |
{ | |
var locked2 = image2.LockBits(new Rectangle(0, 0, target.Width, target.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); | |
try | |
{ | |
var target1 = locked.Scan0; | |
var src1 = locked1.Scan0; | |
var src2 = locked2.Scan0; | |
// Declare an array to hold the bytes of the bitmap. | |
int bytes = Math.Abs(locked.Stride) * locked.Height; | |
byte[] rgbValues = new byte[bytes]; | |
// Copy the RGB values into the array. | |
System.Runtime.InteropServices.Marshal.Copy(target1, rgbValues, 0, bytes); | |
// Set every third value to 255. A 24bpp bitmap will look red. | |
for (int counter = 2; counter < rgbValues.Length; counter += 3) | |
rgbValues[counter] = (byte)(counter%256); | |
// Copy the RGB values back to the bitmap | |
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, target1, bytes); | |
} | |
finally | |
{ | |
image2.UnlockBits(locked2); | |
} | |
} | |
finally | |
{ | |
image1.UnlockBits(locked1); | |
} | |
} | |
finally | |
{ | |
target.UnlockBits(locked); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment