using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ImageLoader
{
public partial class Form1 : Form
{
private Rectangle selectedArea;
private Image loadedImage;
private Color selectionColor;
private int count = 10;
//Get a contrasting color for the image selection marker
using (Bitmap bmp = new Bitmap(loadedImage))
{
selectionColor = GetDominantColor(bmp, false);
selectionColor = CalculateOppositeColor(selectionColor);
}
///tZoom.Value = 1;
//resizePictureArea();
//Map the area selected in the thumbail to the actual image size
Rectangle zoomArea = new Rectangle();
Rectangle localArea = new Rectangle();
private Color GetDominantColor(Bitmap bmp, bool includeAlpha)
{
// GDI+ still lies to us - the return format is BGRA, NOT ARGB.
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
int stride = bmData.Stride;
IntPtr Scan0 = bmData.Scan0;
int r = 0;
int g = 0;
int b = 0;
int a = 0;
int total = 0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - bmp.Width * 4;
int nWidth = bmp.Width;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < nWidth; x++)
{
r += p[0];
g += p[1];
b += p[2];
a += p[3];
/// <summary>
/// Calculates the opposite color of a given color.
/// Source: http://dotnetpulse.blogspot.com/2007/01/function-to-calculate-opposite-color.html
/// </summary>
/// <param name="clr"></param>
/// <returns></returns>
private Color CalculateOppositeColor(Color clr)
{
return Color.FromArgb(255 - clr.R, 255 - clr.G, 255 - clr.B);
}
/// <summary>
/// Constricts a set of given dimensions while keeping aspect ratio.
/// </summary>
private Size ShrinkToDimensions(int originalWidth, int originalHeight, int maxWidth, int maxHeight)
{
int newWidth = 0;
int newHeight = 0;
if (originalWidth >= originalHeight)
{
//Match area width to max width
if (originalWidth <= maxWidth)
{
newWidth = originalWidth;
newHeight = originalHeight;
}
else
{
newWidth = maxWidth;
newHeight = originalHeight * maxWidth / originalWidth;
}
}
else
{
//Match area height to max height
if (originalHeight <= maxHeight)
{
newWidth = originalWidth;
newHeight = originalHeight;
}
else
{
newWidth = originalWidth * maxHeight / originalHeight;
newHeight = maxHeight;
}
}