Dawid Ireno

Dawid Ireno Software Architect

Temat: Algorytmy Resizowania Obrazów w C#

Bitmap Resize (Image i, int width, int heigh)
{
Bitmap b = new Bitmap(i);
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage((Image)result))
{
g.DrawImage(b, 0, 0, width, height);
}
b.Dispose();
return result;
}Dawid Ireno edytował(a) ten post dnia 13.01.08 o godzinie 19:13
Dawid Ireno

Dawid Ireno Software Architect

Temat: Algorytmy Resizowania Obrazów w C#

Bitmap Resize(Image i, int width, int height)
{
Bitmap b = new Bitmap(i);
Bitmap result = new Bitmap(width, height);
double xProp = (double)b.Width / (double)width;
double yProp = (double)b.Height / (double)height;
int x = 0;
for (double xPos = 0; x < width; x++, xPos += xProp)
{
int y = 0;
for (double yPos = 0; y < height; y++, yPos += yProp)
{
RealPoint lowerLeftCorner = new RealPoint(xPos, yPos);
RealPoint downRightCorner = new RealPoint(xPos + xProp, yPos + yProp);
Color c = BitmapHelper.ComputeAvgColor(ref b, lowerLeftCorner, downRightCorner);
result.SetPixel(x, y, c);
}
}
b.Dispose();
return result;
}

==================================================================

using System;
using System.Collections.Generic;
using System.Text;

namespace System.Drawing
{
public class RealPoint
{
private double _X;
private double _Y;

public double X
{
get
{
return this._X;
}
set
{
this._X = value;
}
}

public double Y
{
get
{
return this._Y;
}
set
{
this._Y = value;
}
}

public RealPoint()
{
}

public RealPoint(double x, double y)
{
this.X = x;
this.Y = y;
}
}
}

==================================================================

using System;
using System.Collections.Generic;
using System.Text;

namespace System.Drawing
{
public class BitmapHelper
{
private static double NextX(double x, RealPoint p1, RealPoint p2)
{
if (x != Math.Ceiling(x))
{
return Math.Min(Math.Ceiling((double)p1.X), (double)p2.X);
}
else
{
return Math.Min(x + 1, p2.X);
}
}

private static double NextY(double y, RealPoint p1, RealPoint p2)
{
if (y != Math.Ceiling(y))
{
return Math.Min(Math.Ceiling((double)p1.Y), (double)p2.Y);
}
else
{
return Math.Min(y + 1, p2.Y);
}
}

/// <summary>
/// Computers avg color from bitmap area.
/// </summary>
/// <param name="b">Input bitmap.</param>
/// <param name="p1">Lower left corner</param>
/// <param name="p2">Down right corner</param>
/// <returns></returns>
public static Color ComputeAvgColor(ref Bitmap b, RealPoint p1, RealPoint p2)
{
double totalR = 0;
double totalG = 0;
double totalB = 0;
double counter = 0;
for (double x = p1.X; x < p2.X; )
{
double nextX = BitmapHelper.NextX(x, p1, p2);
for (double y = p1.Y; y < p2.Y; )
{
double nextY = BitmapHelper.NextY(y, p1, p2);
Point current = new Point();
current.X = (int)Math.Floor(x);
current.Y = (int)Math.Floor(y);
Color currentColor = b.GetPixel(current.X, current.Y);
double percentageArea = (nextX - x) * (nextY - y);
totalR += currentColor.R * percentageArea;
totalG += currentColor.G * percentageArea;
totalB += currentColor.B * percentageArea;
counter += percentageArea;
y = nextY;
}
x = nextX;
}
int R = (int)(totalR / counter);
int G = (int)(totalG / counter);
int B = (int)(totalB / counter);
return Color.FromArgb(R, G, B);
}
}
}Dawid Ireno edytował(a) ten post dnia 13.01.08 o godzinie 22:10

Następna dyskusja:

Algorytmy AI i generowanie ...




Wyślij zaproszenie do