本文整理汇总了C#中System.Drawing.Bitmap.UnlockImage方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.UnlockImage方法的具体用法?C# Bitmap.UnlockImage怎么用?C# Bitmap.UnlockImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了Bitmap.UnlockImage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
/// <summary>
/// Generates a cellular texture image
/// </summary>
/// <param name="Width">Width</param>
/// <param name="Height">Height</param>
/// <param name="NumberOfPoints">Number of points</param>
/// <param name="Seed">Random seed</param>
/// <returns>Returns an image of a cellular texture</returns>
public static Bitmap Generate(int Width, int Height, int NumberOfPoints, int Seed)
{
float[,] DistanceBuffer = new float[Width, Height];
float MinimumDistance = float.MaxValue;
float MaxDistance = float.MinValue;
CellularMap Map = new CellularMap(Seed, Width, Height, NumberOfPoints);
MaxDistance = Map.MaxDistance;
MinimumDistance = Map.MinDistance;
DistanceBuffer = Map.Distances;
Bitmap ReturnValue = new Bitmap(Width, Height);
BitmapData ImageData = ReturnValue.LockImage();
int ImagePixelSize = ImageData.GetPixelSize();
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
float Value = GetHeight(x, y, DistanceBuffer, MinimumDistance, MaxDistance);
Value *= 255;
int RGBValue = ((int)Value).Clamp(255, 0);
ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
}
}
ReturnValue.UnlockImage(ImageData);
return ReturnValue;
}
示例2: Generate
/// <summary>
/// Generates a number of faults, returning an image
/// </summary>
/// <param name="Width">Width of the resulting image</param>
/// <param name="Height">Height of the resulting image</param>
/// <param name="NumberFaults">Number of faults</param>
/// <param name="Seed">Random seed</param>
/// <returns>An image from the resulting faults</returns>
public static Bitmap Generate(int Width,int Height,int NumberFaults,int Seed)
{
float[,] Heights = new float[Width, Height];
float IncreaseVal = 0.1f;
System.Random Generator = new System.Random(Seed);
for (int x = 0; x < NumberFaults; ++x)
{
IncreaseVal = GenerateFault(Width, Height, NumberFaults, Heights, IncreaseVal, Generator);
}
Bitmap ReturnValue = new Bitmap(Width, Height);
BitmapData ImageData = ReturnValue.LockImage();
int ImagePixelSize = ImageData.GetPixelSize();
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
float Value = Heights[x, y];
Value = (Value * 0.5f) + 0.5f;
Value *= 255;
int RGBValue = ((int)Value).Clamp(255, 0);
ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
}
}
ReturnValue.UnlockImage(ImageData);
return ReturnValue;
}
示例3: Generate
/// <summary>
/// Generates perlin noise
/// </summary>
/// <param name="Width">Width of the resulting image</param>
/// <param name="Height">Height of the resulting image</param>
/// <param name="MaxRGBValue">MaxRGBValue</param>
/// <param name="MinRGBValue">MinRGBValue</param>
/// <param name="Frequency">Frequency</param>
/// <param name="Amplitude">Amplitude</param>
/// <param name="Persistance">Persistance</param>
/// <param name="Octaves">Octaves</param>
/// <param name="Seed">Random seed</param>
/// <returns>An image containing perlin noise</returns>
public static Bitmap Generate(int Width, int Height, int MaxRGBValue, int MinRGBValue,
float Frequency, float Amplitude, float Persistance, int Octaves, int Seed)
{
Bitmap ReturnValue = new Bitmap(Width, Height);
BitmapData ImageData = ReturnValue.LockImage();
int ImagePixelSize = ImageData.GetPixelSize();
float[,] Noise = GenerateNoise(Seed, Width, Height);
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
float Value = GetValue(x, y, Width, Height, Frequency, Amplitude, Persistance, Octaves, Noise);
Value = (Value * 0.5f) + 0.5f;
Value *= 255;
int RGBValue = ((int)Value).Clamp(MaxRGBValue, MinRGBValue);
ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
}
}
ReturnValue.UnlockImage(ImageData);
return ReturnValue;
}
示例4: OilPainting
public static Bitmap OilPainting(this Bitmap Image, int Seed, int NumberOfPoints=100)
{
Image.ThrowIfNull("Image");
Bitmap _Image = new Bitmap(Image);
CellularMap Map = new CellularMap(Seed, Image.Width, Image.Height, NumberOfPoints);
BitmapData ImageData = _Image.LockImage();
int ImagePixelSize = ImageData.GetPixelSize();
int Width = _Image.Width;
int Height = _Image.Height;
Parallel.For(0, NumberOfPoints, i =>
{
int Red = 0;
int Green = 0;
int Blue = 0;
int Counter = 0;
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
if (Map.ClosestPoint[x,y] == i)
{
Color Pixel = ImageData.GetPixel(x, y, ImagePixelSize);
Red += Pixel.R;
Green += Pixel.G;
Blue += Pixel.B;
++Counter;
}
}
}
int Counter2 = 0;
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
if (Map.ClosestPoint[x,y] == i)
{
ImageData.SetPixel(x, y, Color.FromArgb(Red / Counter, Green / Counter, Blue / Counter), ImagePixelSize);
++Counter2;
if (Counter2 == Counter)
break;
}
}
if (Counter2 == Counter)
break;
}
});
_Image.UnlockImage(ImageData);
return _Image;
}
示例5: EdgeDetection
public static Bitmap EdgeDetection(this Bitmap OriginalImage, float Threshold, Color EdgeColor, string FileName = "")
{
OriginalImage.ThrowIfNull("OriginalImage");
EdgeColor.ThrowIfNull("EdgeColor");
ImageFormat FormatUsing = FileName.GetImageFormat();
Bitmap NewBitmap = new Bitmap(OriginalImage, OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData = OriginalImage.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize = OldData.GetPixelSize();
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
Color CurrentColor = OldData.GetPixel(x, y, OldPixelSize);
if (y < Height - 1 && x < Width - 1)
{
Color TempColor = OldData.GetPixel(x + 1, y + 1, OldPixelSize);
if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
NewData.SetPixel(x, y, EdgeColor, NewPixelSize);
}
else if (y < Height - 1)
{
Color TempColor = OldData.GetPixel(x, y + 1, OldPixelSize);
if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
NewData.SetPixel(x, y, EdgeColor, NewPixelSize);
}
else if (x < Width - 1)
{
Color TempColor = OldData.GetPixel(x + 1, y, OldPixelSize);
if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
NewData.SetPixel(x, y, EdgeColor, NewPixelSize);
}
}
});
NewBitmap.UnlockImage(NewData);
OriginalImage.UnlockImage(OldData);
if (!string.IsNullOrEmpty(FileName))
NewBitmap.Save(FileName, FormatUsing);
return NewBitmap;
}
示例6: MotionDetection
/// <summary>
/// Runs a simplistic motion detection algorithm
/// </summary>
/// <param name="NewImage">The "new" frame</param>
/// <param name="OldImage">The "old" frame</param>
/// <param name="Threshold">The threshold used to detect changes in the image</param>
/// <param name="DetectionColor">Color to display changes in the images as</param>
/// <returns>A bitmap indicating where changes between frames have occurred overlayed on top of the new image.</returns>
public static Bitmap MotionDetection(this Bitmap NewImage, Bitmap OldImage, int Threshold, Color DetectionColor)
{
NewImage.ThrowIfNull("NewImage");
OldImage.ThrowIfNull("OldImage");
DetectionColor.ThrowIfNull("DetectionColor");
using (Bitmap NewImage1 = NewImage.BlackAndWhite())
{
using (Bitmap OldImage1 = OldImage.BlackAndWhite())
{
using (Bitmap NewImage2 = NewImage1.SNNBlur(5))
{
using (Bitmap OldImage2 = OldImage1.SNNBlur(5))
{
using (Bitmap OutputImage = new Bitmap(NewImage2, NewImage2.Width, NewImage2.Height))
{
using (Bitmap Overlay = new Bitmap(NewImage, NewImage.Width, NewImage.Height))
{
BitmapData NewImage2Data = NewImage2.LockImage();
int NewImage2PixelSize = NewImage2Data.GetPixelSize();
BitmapData OldImage2Data = OldImage2.LockImage();
int OldImage2PixelSize = OldImage2Data.GetPixelSize();
BitmapData OverlayData = Overlay.LockImage();
int OverlayPixelSize = OverlayData.GetPixelSize();
int Width = OutputImage.Width;
int Height = OutputImage.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
Color NewPixel = NewImage2Data.GetPixel(x, y, NewImage2PixelSize);
Color OldPixel = OldImage2Data.GetPixel(x, y, OldImage2PixelSize);
if (System.Math.Pow((double)(NewPixel.R - OldPixel.R), 2.0) > Threshold)
{
OverlayData.SetPixel(x, y, Color.FromArgb(100, 0, 100), OverlayPixelSize);
}
else
{
OverlayData.SetPixel(x, y, Color.FromArgb(200, 0, 200), OverlayPixelSize);
}
}
});
Overlay.UnlockImage(OverlayData);
NewImage2.UnlockImage(NewImage2Data);
OldImage2.UnlockImage(OldImage2Data);
using (Bitmap Overlay2 = Overlay.EdgeDetection(25, DetectionColor))
{
BitmapData Overlay2Data = Overlay2.LockImage();
int Overlay2PixelSize = Overlay2Data.GetPixelSize();
Width = OutputImage.Width;
Height = OutputImage.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
Color Pixel1 = Overlay2Data.GetPixel(x, y, Overlay2PixelSize);
if (Pixel1.R != DetectionColor.R || Pixel1.G != DetectionColor.G || Pixel1.B != DetectionColor.B)
{
Overlay2Data.SetPixel(x, y, Color.FromArgb(200, 0, 200), Overlay2PixelSize);
}
}
});
Overlay2.UnlockImage(Overlay2Data);
return OutputImage.Watermark(Overlay2, 1.0f, 0, 0, Color.FromArgb(200, 0, 200));
}
}
}
}
}
}
}
}
示例7: AdjustGamma
public static Bitmap AdjustGamma(Bitmap OriginalImage, float Value)
{
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();//Image.LockImage(NewBitmap);
BitmapData OldData = OriginalImage.LockImage();//Image.LockImage(OriginalImage);
int NewPixelSize = NewData.GetPixelSize();//Image.GetPixelSize(NewData);
int OldPixelSize = OldData.GetPixelSize();//Image.GetPixelSize(OldData);
int[] RedRamp = new int[256];
int[] GreenRamp = new int[256];
int[] BlueRamp = new int[256];
for (int x = 0; x < 256; ++x)
{
RedRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
GreenRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
BlueRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
}
for (int x = 0; x < NewBitmap.Width; ++x)
{
for (int y = 0; y < NewBitmap.Height; ++y)
{
Color Pixel = OldData.GetPixel(x, y, OldPixelSize);//Image.GetPixel(OldData, x, y, OldPixelSize);
int Red = RedRamp[Pixel.R];
int Green = GreenRamp[Pixel.G];
int Blue = BlueRamp[Pixel.B];
NewData.SetPixel(x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);//Image.SetPixel(NewData, x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);
}
}
NewBitmap.UnlockImage(NewData);//Image.UnlockImage(NewBitmap, NewData);
OriginalImage.UnlockImage(OldData);//Image.UnlockImage(OriginalImage, OldData);
return NewBitmap;
}
示例8: LoadImage
/// <summary>
/// Loads an image
/// </summary>
/// <param name="ImageUsing">Image to load</param>
public virtual void LoadImage(Bitmap ImageUsing)
{
ImageUsing.ThrowIfNull("ImageUsing");
BitmapData OldData = ImageUsing.LockImage();
int PixelSize = OldData.GetPixelSize();
Width = ImageUsing.Width;
Height = ImageUsing.Height;
R.Clear();
G.Clear();
B.Clear();
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
Color TempColor = OldData.GetPixel(x, y, PixelSize);
++R[(int)TempColor.R];
++G[(int)TempColor.G];
++B[(int)TempColor.B];
}
}
ImageUsing.UnlockImage(OldData);
}
示例9: Process
/// <summary>
/// Runs the motion detection algorithm
/// </summary>
/// <param name="NewImage">The "new" frame</param>
/// <param name="OldImage">The "old" frame</param>
/// <param name="Threshold">The threshold used to detect changes in the image</param>
/// <param name="DetectionColor">Color to display changes in the images as</param>
/// <returns>A bitmap indicating where changes between frames have occurred overlayed on top of the new image.</returns>
public static Bitmap Process(Bitmap NewImage, Bitmap OldImage, int Threshold, Color DetectionColor)
{
if (NewImage == null)
throw new ArgumentNullException("NewImage");
if (OldImage == null)
throw new ArgumentNullException("OldImage");
if (DetectionColor == null)
throw new ArgumentNullException("DetectionColor");
using (Bitmap NewImage1 = NewImage.BlackAndWhite())
{
using (Bitmap OldImage1 = OldImage.BlackAndWhite())
{
using (Bitmap NewImage2 = NewImage1.SNNBlur(5))
{
using (Bitmap OldImage2 = OldImage1.SNNBlur(5))
{
using (Bitmap OutputImage = new Bitmap(NewImage2, NewImage2.Width, NewImage2.Height))
{
using (Bitmap Overlay = new Bitmap(NewImage, NewImage.Width, NewImage.Height))
{
BitmapData NewImage2Data = NewImage2.LockImage();
int NewImage2PixelSize = NewImage2Data.GetPixelSize();
BitmapData OldImage2Data = OldImage2.LockImage();
int OldImage2PixelSize = OldImage2Data.GetPixelSize();
BitmapData OverlayData = Overlay.LockImage();
int OverlayPixelSize = OverlayData.GetPixelSize();
for (int x = 0; x < OutputImage.Width; ++x)
{
for (int y = 0; y < OutputImage.Height; ++y)
{
Color NewPixel = NewImage2Data.GetPixel(x, y, NewImage2PixelSize);
Color OldPixel = OldImage2Data.GetPixel(x, y, OldImage2PixelSize);
if (System.Math.Pow((double)(NewPixel.R - OldPixel.R), 2.0) > Threshold)
{
OverlayData.SetPixel(x, y, Color.FromArgb(100, 0, 100), OverlayPixelSize);
}
else
{
OverlayData.SetPixel(x, y, Color.FromArgb(200, 0, 200), OverlayPixelSize);
}
}
}
Overlay.UnlockImage(OverlayData);
NewImage2.UnlockImage(NewImage2Data);
OldImage2.UnlockImage(OldImage2Data);
using (Bitmap Overlay2 = Overlay.EdgeDetection(25, DetectionColor))
{
BitmapData Overlay2Data = Overlay2.LockImage();
int Overlay2PixelSize = Overlay2Data.GetPixelSize();
for (int x = 0; x < OutputImage.Width; ++x)
{
for (int y = 0; y < OutputImage.Height; ++y)
{
Color Pixel1 = Overlay2Data.GetPixel(x, y, Overlay2PixelSize);
if (Pixel1.R != DetectionColor.R || Pixel1.G != DetectionColor.G || Pixel1.B != DetectionColor.B)
{
Overlay2Data.SetPixel(x, y, Color.FromArgb(200, 0, 200), Overlay2PixelSize);
}
}
}
Overlay2.UnlockImage(Overlay2Data);
return OutputImage.Watermark(Overlay2, 1.0f, 0, 0, Color.FromArgb(200, 0, 200));
}
}
}
}
}
}
}
}
示例10: StretchContrast
public static Bitmap StretchContrast(this Bitmap OriginalImage, string FileName = "")
{
OriginalImage.ThrowIfNull("OriginalImage");
ImageFormat FormatUsing = FileName.GetImageFormat();
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData = OriginalImage.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize = OldData.GetPixelSize();
Color MinValue;
Color MaxValue;
GetMinMaxPixel(out MinValue, out MaxValue, OldData, OldPixelSize);
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize);
Color TempValue = Color.FromArgb(Map(CurrentPixel.R, MinValue.R, MaxValue.R),
Map(CurrentPixel.G, MinValue.G, MaxValue.G),
Map(CurrentPixel.B, MinValue.B, MaxValue.B));
NewData.SetPixel(x, y, TempValue, NewPixelSize);
}
});
NewBitmap.UnlockImage(NewData);
OriginalImage.UnlockImage(OldData);
if (!string.IsNullOrEmpty(FileName))
NewBitmap.Save(FileName, FormatUsing);
return NewBitmap;
}
示例11: KuwaharaBlur
public static Bitmap KuwaharaBlur(this Bitmap OriginalImage, int Size = 3, string FileName = "")
{
OriginalImage.ThrowIfNull("OriginalImage");
ImageFormat FormatUsing = FileName.GetImageFormat();
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData = OriginalImage.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize = OldData.GetPixelSize();
int[] ApetureMinX = { -(Size / 2), 0, -(Size / 2), 0 };
int[] ApetureMaxX = { 0, (Size / 2), 0, (Size / 2) };
int[] ApetureMinY = { -(Size / 2), -(Size / 2), 0, 0 };
int[] ApetureMaxY = { 0, 0, (Size / 2), (Size / 2) };
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
int[] RValues = { 0, 0, 0, 0 };
int[] GValues = { 0, 0, 0, 0 };
int[] BValues = { 0, 0, 0, 0 };
int[] NumPixels = { 0, 0, 0, 0 };
int[] MaxRValue = { 0, 0, 0, 0 };
int[] MaxGValue = { 0, 0, 0, 0 };
int[] MaxBValue = { 0, 0, 0, 0 };
int[] MinRValue = { 255, 255, 255, 255 };
int[] MinGValue = { 255, 255, 255, 255 };
int[] MinBValue = { 255, 255, 255, 255 };
for (int i = 0; i < 4; ++i)
{
for (int x2 = ApetureMinX[i]; x2 < ApetureMaxX[i]; ++x2)
{
int TempX = x + x2;
if (TempX >= 0 && TempX < Width)
{
for (int y2 = ApetureMinY[i]; y2 < ApetureMaxY[i]; ++y2)
{
int TempY = y + y2;
if (TempY >= 0 && TempY < Height)
{
Color TempColor = OldData.GetPixel(TempX, TempY, OldPixelSize);
RValues[i] += TempColor.R;
GValues[i] += TempColor.G;
BValues[i] += TempColor.B;
if (TempColor.R > MaxRValue[i])
MaxRValue[i] = TempColor.R;
else if (TempColor.R < MinRValue[i])
MinRValue[i] = TempColor.R;
if (TempColor.G > MaxGValue[i])
MaxGValue[i] = TempColor.G;
else if (TempColor.G < MinGValue[i])
MinGValue[i] = TempColor.G;
if (TempColor.B > MaxBValue[i])
MaxBValue[i] = TempColor.B;
else if (TempColor.B < MinBValue[i])
MinBValue[i] = TempColor.B;
++NumPixels[i];
}
}
}
}
}
int j = 0;
int MinDifference = 10000;
for (int i = 0; i < 4; ++i)
{
int CurrentDifference = (MaxRValue[i] - MinRValue[i]) + (MaxGValue[i] - MinGValue[i]) + (MaxBValue[i] - MinBValue[i]);
if (CurrentDifference < MinDifference && NumPixels[i] > 0)
{
j = i;
MinDifference = CurrentDifference;
}
}
Color MeanPixel = Color.FromArgb(RValues[j] / NumPixels[j],
GValues[j] / NumPixels[j],
BValues[j] / NumPixels[j]);
NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
}
});
NewBitmap.UnlockImage(NewData);
OriginalImage.UnlockImage(OldData);
if (!string.IsNullOrEmpty(FileName))
NewBitmap.Save(FileName, FormatUsing);
return NewBitmap;
}
示例12: AdjustContrast
public static Bitmap AdjustContrast(this Bitmap OriginalImage, float Value = 0, string FileName = "")
{
if (OriginalImage == null)
throw new ArgumentNullException("OriginalImage");
ImageFormat FormatUsing = FileName.GetImageFormat();
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData = OriginalImage.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize = OldData.GetPixelSize();
Value = (100.0f + Value) / 100.0f;
Value *= Value;
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
Color Pixel = OldData.GetPixel(x, y, OldPixelSize);
float Red = Pixel.R / 255.0f;
float Green = Pixel.G / 255.0f;
float Blue = Pixel.B / 255.0f;
Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f;
Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f;
Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f;
NewData.SetPixel(x, y,
Color.FromArgb(((int)Red).Clamp(255, 0),
((int)Green).Clamp(255, 0),
((int)Blue).Clamp(255, 0)),
NewPixelSize);
}
});
NewBitmap.UnlockImage(NewData);
OriginalImage.UnlockImage(OldData);
if (!string.IsNullOrEmpty(FileName))
NewBitmap.Save(FileName, FormatUsing);
return NewBitmap;
}
示例13: SobelEdgeDetection
/// <summary>
/// Sobel edge detection function
/// </summary>
/// <param name="Input">Image to manipulate</param>
/// <param name="FileName">File to save to</param>
/// <returns>A bitmap image</returns>
public static Bitmap SobelEdgeDetection(this Bitmap Input, string FileName = "")
{
Input.ThrowIfNull("Input");
ImageFormat FormatUsing = FileName.GetImageFormat();
using (Bitmap TempImage = Input.BlackAndWhite())
{
Filter TempFilter = new Filter(3, 3);
TempFilter.MyFilter[0, 0] = -1;
TempFilter.MyFilter[0, 1] = 0;
TempFilter.MyFilter[0, 2] = 1;
TempFilter.MyFilter[1, 0] = -2;
TempFilter.MyFilter[1, 1] = 0;
TempFilter.MyFilter[1, 2] = 2;
TempFilter.MyFilter[2, 0] = -1;
TempFilter.MyFilter[2, 1] = 0;
TempFilter.MyFilter[2, 2] = 1;
TempFilter.Absolute = true;
using (Bitmap TempImageX = TempFilter.ApplyFilter(TempImage))
{
TempFilter = new Filter(3, 3);
TempFilter.MyFilter[0, 0] = 1;
TempFilter.MyFilter[0, 1] = 2;
TempFilter.MyFilter[0, 2] = 1;
TempFilter.MyFilter[1, 0] = 0;
TempFilter.MyFilter[1, 1] = 0;
TempFilter.MyFilter[1, 2] = 0;
TempFilter.MyFilter[2, 0] = -1;
TempFilter.MyFilter[2, 1] = -2;
TempFilter.MyFilter[2, 2] = -1;
TempFilter.Absolute = true;
using (Bitmap TempImageY = TempFilter.ApplyFilter(TempImage))
{
using (Bitmap NewBitmap = new Bitmap(TempImage.Width, TempImage.Height))
{
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData1 = TempImageX.LockImage();
BitmapData OldData2 = TempImageY.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize1 = OldData1.GetPixelSize();
int OldPixelSize2 = OldData2.GetPixelSize();
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
Color Pixel1 = OldData1.GetPixel(x, y, OldPixelSize1);
Color Pixel2 = OldData2.GetPixel(x, y, OldPixelSize2);
NewData.SetPixel(x, y,
Color.FromArgb((Pixel1.R + Pixel2.R).Clamp(255, 0),
(Pixel1.G + Pixel2.G).Clamp(255, 0),
(Pixel1.B + Pixel2.B).Clamp(255, 0)),
NewPixelSize);
}
});
NewBitmap.UnlockImage(NewData);
TempImageX.UnlockImage(OldData1);
TempImageY.UnlockImage(OldData2);
Bitmap NewBitmap2 = NewBitmap.Negative();
if (!string.IsNullOrEmpty(FileName))
NewBitmap2.Save(FileName, FormatUsing);
return NewBitmap2;
}
}
}
}
}
示例14: SNNBlur
public static Bitmap SNNBlur(this Bitmap OriginalImage, int Size = 3, string FileName = "")
{
OriginalImage.ThrowIfNull("OriginalImage");
ImageFormat FormatUsing = FileName.GetImageFormat();
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData = OriginalImage.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize = OldData.GetPixelSize();
int ApetureMinX = -(Size / 2);
int ApetureMaxX = (Size / 2);
int ApetureMinY = -(Size / 2);
int ApetureMaxY = (Size / 2);
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
int RValue = 0;
int GValue = 0;
int BValue = 0;
int NumPixels = 0;
for (int x2 = ApetureMinX; x2 < ApetureMaxX; ++x2)
{
int TempX1 = x + x2;
int TempX2 = x - x2;
if (TempX1 >= 0 && TempX1 < Width && TempX2 >= 0 && TempX2 < Width)
{
for (int y2 = ApetureMinY; y2 < ApetureMaxY; ++y2)
{
int TempY1 = y + y2;
int TempY2 = y - y2;
if (TempY1 >= 0 && TempY1 < Height && TempY2 >= 0 && TempY2 < Height)
{
Color TempColor = OldData.GetPixel(x, y, OldPixelSize);
Color TempColor2 = OldData.GetPixel(TempX1, TempY1, OldPixelSize);
Color TempColor3 = OldData.GetPixel(TempX2, TempY2, OldPixelSize);
if (Distance(TempColor.R, TempColor2.R, TempColor.G, TempColor2.G, TempColor.B, TempColor2.B) <
Distance(TempColor.R, TempColor3.R, TempColor.G, TempColor3.G, TempColor.B, TempColor3.B))
{
RValue += TempColor2.R;
GValue += TempColor2.G;
BValue += TempColor2.B;
}
else
{
RValue += TempColor3.R;
GValue += TempColor3.G;
BValue += TempColor3.B;
}
++NumPixels;
}
}
}
}
Color MeanPixel = Color.FromArgb(RValue / NumPixels,
GValue / NumPixels,
BValue / NumPixels);
NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
}
});
NewBitmap.UnlockImage(NewData);
OriginalImage.UnlockImage(OldData);
if (!string.IsNullOrEmpty(FileName))
NewBitmap.Save(FileName, FormatUsing);
return NewBitmap;
}
示例15: SinWave
public static Bitmap SinWave(this Bitmap OriginalImage, float Amplitude, float Frequency, bool XDirection, bool YDirection, string FileName = "")
{
OriginalImage.ThrowIfNull("OriginalImage");
ImageFormat FormatUsing = FileName.GetImageFormat();
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = NewBitmap.LockImage();
BitmapData OldData = OriginalImage.LockImage();
int NewPixelSize = NewData.GetPixelSize();
int OldPixelSize = OldData.GetPixelSize();
int Width = NewBitmap.Width;
int Height = NewBitmap.Height;
Parallel.For(0, Width, x =>
{
for (int y = 0; y < Height; ++y)
{
double Value1 = 0;
double Value2 = 0;
if (YDirection)
Value1 = System.Math.Sin(((x * Frequency) * System.Math.PI) / 180.0d) * Amplitude;
if (XDirection)
Value2 = System.Math.Sin(((y * Frequency) * System.Math.PI) / 180.0d) * Amplitude;
Value1 = y - (int)Value1;
Value2 = x - (int)Value2;
while (Value1 < 0)
Value1 += Height;
while (Value2 < 0)
Value2 += Width;
while (Value1 >= Height)
Value1 -= Height;
while (Value2 >= Width)
Value2 -= Width;
NewData.SetPixel(x, y,
OldData.GetPixel((int)Value2, (int)Value1, OldPixelSize),
NewPixelSize);
}
});
NewBitmap.UnlockImage(NewData);
OriginalImage.UnlockImage(OldData);
if (!string.IsNullOrEmpty(FileName))
NewBitmap.Save(FileName, FormatUsing);
return NewBitmap;
}