本文整理汇总了C#中System.Drawing.Bitmap.GetPixel方法的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Bitmap.GetPixel方法的具体用法?C# System.Drawing.Bitmap.GetPixel怎么用?C# System.Drawing.Bitmap.GetPixel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了System.Drawing.Bitmap.GetPixel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeImagesNegative
static void MakeImagesNegative(string sourceDirectoryPath, string targetDirectoryPath)
{
string[] filenames = System.IO.Directory.GetFiles(sourceDirectoryPath);
foreach (var filename in filenames)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filename);
Parallel.For(0, bitmap.Height, (bitmapRowIndex) =>
{
lock (bitmap)
{
for (int bitmapColIndex = 0; bitmapColIndex < bitmap.Width; bitmapColIndex++)
{
var pixel = bitmap.GetPixel(bitmapColIndex, bitmapRowIndex);
var negativePixel = System.Drawing.Color.FromArgb(
byte.MaxValue - pixel.A,
byte.MaxValue - pixel.R,
byte.MaxValue - pixel.G,
byte.MaxValue - pixel.B);
bitmap.SetPixel(bitmapColIndex, bitmapRowIndex, negativePixel);
}
}
});
bitmap.Save(filename.Replace(sourceDirectoryPath, targetDirectoryPath));
}
}
示例2: MeasureDisplayString
public static System.Drawing.SizeF MeasureDisplayString(System.Drawing.Graphics graphics, string text, System.Drawing.Font font)
{
const int width = 32;
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (width, 1, graphics);
System.Drawing.SizeF size = graphics.MeasureString (text, font);
System.Drawing.Graphics anagra = System.Drawing.Graphics.FromImage (bitmap);
int measured_width = (int) size.Width;
if (anagra != null)
{
anagra.Clear (System.Drawing.Color.White);
anagra.DrawString (text+"|", font, System.Drawing.Brushes.Black, width - measured_width, -font.Height / 2);
for (int i = width-1; i >= 0; i--)
{
measured_width--;
if (bitmap.GetPixel (i, 0).R == 0)
{
break;
}
}
}
return new System.Drawing.SizeF (measured_width, size.Height);
}
示例3: TextureData
public TextureData(string src)
{
try
{
System.Drawing.Bitmap texture = new System.Drawing.Bitmap(src);
width = texture.Width;
height = texture.Height;
Clear();
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
Color xnaColor = new Color();
System.Drawing.Color formsColor = texture.GetPixel(x, y);
xnaColor.R = formsColor.R;
xnaColor.G = formsColor.G;
xnaColor.B = formsColor.B;
xnaColor.A = formsColor.A;
data[y * width + x] = xnaColor.PackedValue;
}
}
catch(Exception e)
{
Console.WriteLine("Failed to load file: " + e.Message);
Clear();
}
}
示例4: CreateGradientServer
public static LibNoise.Gradient CreateGradientServer(List<LibNoise.GradientPresets.GradientKeyData> keyData)
{
List<LibNoise.GradientKey> keys = new List<LibNoise.GradientKey>();
for (int i = 0; i < keyData.Count; i++) {
if (keyData[i].isImage) {
List<Color[]> images = new List<Color[]>();
string[] files = keyData[i].imageFiles.ToArray();
for (int j = 0; j < files.Length; j++) {
string file = GameServer.Instance.AppDirectory + "terrainTextures" + Path.DirectorySeparatorChar + files[j];
if (File.Exists(file)) {
System.Drawing.Bitmap map = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(file));
//Logger.Log("{0}: {1} {2}", file, map.Width.ToString(), map.Height.ToString());
System.Drawing.Color[] imgColors = new System.Drawing.Color[map.Width * map.Height];
for (int x = 0; x < map.Width; x++) {
for (int y = 0; y < map.Height; y++) {
imgColors[x + y * map.Width] = map.GetPixel(x, y);
}
}
if (!TextureFiles.ContainsKey(files[j]))
TextureFiles.Add(files[j], imgColors);
keyData[i].images.Add(ColorConvert.LibColList(imgColors));
images.Add(ColorConvert.LibColList(imgColors));
}
}
keys.Add(new LibNoise.GradientKey(images, 10, 10, keyData[i].time));
}
else {
keys.Add(new LibNoise.GradientKey(keyData[i].color, keyData[i].time));
}
}
return LibNoise.GradientPresets.CreateGradient(keys);
}
示例5: Execute
public bool Execute()
{
System.Drawing.Bitmap image = new System.Drawing.Bitmap(this.Arguments[1]);
if (this.Arguments.Length >= Parameters.Length)
{
System.Drawing.Color c;
int x = int.Parse(this.Arguments[2]);
int y = int.Parse(this.Arguments[3]);
int z = int.Parse(this.Arguments[4]);
for (int i = 0; i < image.Width; i++)
{
for (int j = 0; j < image.Height; j++)
{
c = image.GetPixel(i, j);
Sandvox.BlockGame.world.Add( new Volume( new Vector3(i + x, image.Height - j + y, z), new Vector3(1, 1, 1), new Color(c.R, c.G, c.B)));
}
}
Output += this.Arguments[1] + "(" + image.Width + "," + image.Height + ") summoned at (" + this.Arguments[2] + "," + this.Arguments[3] + "," + this.Arguments[4] + ")";
return true;
}
else
Output = "Invalid parameters.\n" + HelpMessage;
return false;
}
示例6: PanRight
public PanRight()
{
string[] res = GetType().Assembly.GetManifestResourceNames();
if(res.GetLength(0) > 0)
{
m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "PanRight.bmp"));
if(m_bitmap != null)
{
m_bitmap.MakeTransparent(m_bitmap.GetPixel(1,1));
m_hBitmap = m_bitmap.GetHbitmap();
}
}
m_pHookHelper = new HookHelperClass ();
}
示例7: BitmapFormat
void BitmapFormat(string path)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(path);
if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
return;
System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
for (int y = 0; y < newBitmap.Height; y++)
for (int x = 0; x < newBitmap.Width; x++)
newBitmap.SetPixel(x, y, bitmap.GetPixel(x, y));
bitmap.Dispose();
if (File.Exists(path))
File.Delete(path);
newBitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
newBitmap.Dispose();
}
示例8: MainWindow
public MainWindow()
{
InitializeComponent();
// Create an instance of the open file dialog box.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
AllocConsole();
//Run alg
Console.WriteLine("Load original...");
System.Drawing.Bitmap org = new System.Drawing.Bitmap(openFileDialog1.FileName);
Original = org.Clone(new System.Drawing.Rectangle(0, 0, org.Width, org.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//Convert to grayscale
Console.WriteLine("Convert original to grayscale");
for (int i = 0; i < Original.Width; i++)
{
for (int x = 0; x < Original.Height; x++)
{
System.Drawing.Color oc = Original.GetPixel(i, x);
int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
System.Drawing.Color nc = System.Drawing.Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
Original.SetPixel(i, x, nc);
}
}
PerformAlgorithm();
this.ImgOrg.Source = BitmapToImageSource(Original);
// FreeConsole();
}
else
{
this.Close();
}
}
示例9: GetAndSetPixelComparingToSystemBitmap
public void GetAndSetPixelComparingToSystemBitmap()
{
using (var systemBitmap = new System.Drawing.Bitmap(DefaultWidth, DefaultHeight))
{
int x = 0;
int y = 0;
systemBitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(DefaultColor.ARGB));
bmp.Set(x, y, new Color(DefaultColorAsInt));
var retrivedColor = systemBitmap.GetPixel(x, y);
var retrievedColorAsInt = bmp.Get(x, y);
Assert.AreEqual(retrivedColor.ToArgb(), retrievedColorAsInt.ARGB);
}
}
示例10: IsColorful
private bool IsColorful(Uri url)
{
using (Stream stream = Helper.GetFile(url))
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
float saturation = 0;
int threshold = 100;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
saturation += bmp.GetPixel(x, y).GetSaturation();
if (saturation > threshold) {
return true;
};
}
}
}
return false;
}
示例11: GetMaskIPictureDispFromBitmap
public static stdole.IPictureDisp GetMaskIPictureDispFromBitmap(System.Drawing.Bitmap bmp)
{
System.Drawing.Bitmap mask = new System.Drawing.Bitmap(bmp);
try
{
for (int x = 0; x < mask.Width; x++)
{
for (int y = 0; y < mask.Height; y++)
{
if (mask.GetPixel(x, y).A == 0)
mask.SetPixel(x, y, System.Drawing.Color.White);
else
mask.SetPixel(x, y, System.Drawing.Color.Black);
}
}
return GetIPictureDispFromImage(mask);
}
finally
{
mask.Dispose();
}
}
示例12: BuildTexture
/// <summary>
/// 建立贴图并添加到缓冲中。
/// 尽量在第一次绘制之前调用该函数,这样可以避免建立贴图的过程造成游戏的停滞
/// </summary>
/// <param name="text">要创建的字符串</param>
/// <param name="fontName">字体</param>
/// <returns></returns>
public Texture2D BuildTexture( string text, string fontName )
{
TextKey key = new TextKey( text, fontName );
if (cache.ContainsKey( key ))
return null;
if (!fonts.ContainsKey( fontName ))
{
Log.Write( "error fontName used in BuildTexture" );
return null;
}
System.Drawing.Font font = fonts[fontName];
System.Drawing.SizeF size = mesureGraphics.MeasureString( text, font );
int texWidth = (int)size.Width;
int texHeight = (int)size.Height;
System.Drawing.Bitmap textMap = new System.Drawing.Bitmap( texWidth, texHeight );
System.Drawing.Graphics curGraphics = System.Drawing.Graphics.FromImage( textMap );
curGraphics.DrawString( text, font, System.Drawing.Brushes.White, new System.Drawing.PointF() );
Texture2D texture = new Texture2D( engine.Device, texWidth, texHeight, 1, TextureUsage.None, SurfaceFormat.Color );
Microsoft.Xna.Framework.Graphics.Color[] data = new Microsoft.Xna.Framework.Graphics.Color[texWidth * texHeight];
for (int y = 0; y < texHeight; y++)
{
for (int x = 0; x < texWidth; x++)
{
data[y * texWidth + x] = ConvertHelper.SysColorToXNAColor( textMap.GetPixel( x, y ) );
}
}
texture.SetData<Microsoft.Xna.Framework.Graphics.Color>( data );
cache.Add( key, texture );
curGraphics.Dispose();
return texture;
}
示例13: Parse
public Container Parse(string filename, SxzColor backgroundColor, bool useMostCommonColor, bool parseBackground, BackgroundType backgroundType, double bitsPerPixel, double maximumDistance)
{
int width = 0;
int height = 0;
//now we can act on our pixel byte array to get pixels out
//and also histogram because it is informative if nothing else
LocationPool locationPool = null;
Dictionary<SxzColor, int> histogram = new Dictionary<SxzColor, int>();
TransparentRegion transparentRegion = new TransparentRegion();
//Parse all the pixels into the locationpool, histogram and remove the fully transparent pixels into the transparentregion
using (System.Drawing.Bitmap bitMap = new System.Drawing.Bitmap(filename))
{
width = bitMap.Width;
height = bitMap.Height;
locationPool = new LocationPool(width, height);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
System.Drawing.Color pixel = bitMap.GetPixel(x, y);
Location location = new Location();
location.Color = new SxzColor(pixel.R, pixel.G, pixel.B);
location.Point.X = x;
location.Point.Y = y;
//Don't put transparent colors into the histogram
if (pixel.A == 0)
{
transparentRegion.Add(location);
locationPool.SetLocation(location, x, y);
locationPool.SetMarked(location);
continue;
}
if (histogram.ContainsKey(location.Color))
{
int count = histogram[location.Color];
histogram[location.Color] = count + 1;
}
else
{
histogram.Add(location.Color, 1);
}
locationPool.SetLocation(location, x, y);
}
}
}
Console.WriteLine("Total pixel count " + (width * height));
Console.WriteLine("Total transparent pixel count " + transparentRegion.Locations.Count);
//Remove all the transparent locations
//foreach (Location location in transparentRegion.Locations)
//{
// locationPool.SetMarked(location);
//}
//set the neighbors after removing transparent pixels to save time since those aren't needed anymore
locationPool.SetNeighbors();
Console.WriteLine("Done removing transparent pixels");
//Sort the colors by frequency
List<KeyValuePair<SxzColor, int>> colorList = histogram.ToList();
colorList.Sort((first, second) =>
{
return first.Value == second.Value ? (int)(SxzColor.GetColorDistance(second.Key, new SxzColor(0, 0, 0)) - SxzColor.GetColorDistance(first.Key, new SxzColor(0, 0, 0)))
: second.Value.CompareTo(first.Value);
});
//Find the most commonly used color
SxzColor mostCommonColor = colorList[0].Key;
if (useMostCommonColor)
{
backgroundColor = mostCommonColor;
}
//always start with a palette, register empty palette and fill as we go
Console.WriteLine("Processing the most common color");
DefaultPaletteChunk defaultPaletteChunk = new DefaultPaletteChunk();
Palette defaultPalette = new Palette(defaultPaletteChunk);
//Initialization overhead
List<PaletteContainer> paletteContainers = new List<PaletteContainer>();
PaletteContainer initialPaletteContainer = new PaletteContainer();
initialPaletteContainer.Initial = true;
initialPaletteContainer.Palette = defaultPalette;
paletteContainers.Add(initialPaletteContainer);
HashSet<Location> locations = locationPool.GetByColor(backgroundColor);
if (parseBackground && locations.Count > 0)
{
//.........这里部分代码省略.........
示例14: TestProcessMatrix
public void TestProcessMatrix()
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(4, 4);
for (int x = 0; x < 4; ++x) for (int y = 0; y < 4; ++y) bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(x + y * 4 + 1, x + y * 4 + 1, x + y * 4 + 1));
processor = new FilterProcessing.FilterProcessor(new double[][] {
new double[] {0,0,0},
new double[] {0,0,0},
new double[] {0,1,0}
});
bmp = processor.Process(bmp);
for (int x = 0; x < 3; ++x) for (int y = 0; y < 4; ++y) Assert.IsTrue(bmp.GetPixel(x, y) == System.Drawing.Color.FromArgb(x + y * 4 + 2, x + y * 4 + 2, x + y * 4 + 2));
for (int y = 0; y < 4; ++y) Assert.IsTrue(bmp.GetPixel(3, y) == System.Drawing.Color.FromArgb(255, 0, 0, 0));
}
示例15: ImportImage
public static void ImportImage(Altaxo.Data.DataTable table)
{
ColorAmplitudeFunction colorfunc;
System.IO.Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "Image files (*.bmp;*.jpg;*.png,*.tif)|*.bmp;*.jpg;*.png,*.tif|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = openFileDialog1.OpenFile())!= null)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myStream);
int sizex = bmp.Width;
int sizey = bmp.Height;
//if(Format16bppGrayScale==bmp.PixelFormat)
colorfunc = new ColorAmplitudeFunction(ColorToBrightness);
// add here other function or the result of a dialog box
// now add new columns to the worksheet,
// the name of the columns should preferabbly simply
// the index in x direction
table.Suspend();
for(int i=0;i<sizex;i++)
{
Altaxo.Data.DoubleColumn dblcol = new Altaxo.Data.DoubleColumn();
for(int j=sizey-1;j>=0;j--)
dblcol[j] = colorfunc(bmp.GetPixel(i,j));
table.DataColumns.Add(dblcol,table.DataColumns.FindUniqueColumnName(i.ToString())); // Spalte hinzufügen
} // end for all x coordinaates
table.Resume();
myStream.Close();
myStream=null;
} // end if myStream was != null
}
}