public static long[] GetHistogram(this Bitmap imageBitMap) { long[] histogram = new long[256]; BitmapData bmData = null; int bytesPerPixel, heightInPixels, widthInPixels; try { LockImage(imageBitMap, out bmData, out bytesPerPixel, out heightInPixels, out widthInPixels); unsafe { for (int y = 0; y < heightInPixels; y++) { for (int x = 0; x < widthInPixels; x += bytesPerPixel) { histogram[bmData.GetPixelXY(x, y)]++; } } } imageBitMap.UnlockBits(bmData); } catch { try { imageBitMap.UnlockBits(bmData); } catch { } } return(histogram); }
private void csvToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild == null) { MessageBox.Show("First you need open image to export", "You don't choose the image"); return; } saveFileDialog1.Filter = "CSV FILES | *.csv"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { try { ((ImageForm)this.ActiveMdiChild).MainImage.Save(saveFileDialog1.FileName, ((ImageForm)this.ActiveMdiChild).MainImage.RawFormat); } catch (Exception ex) { MessageBox.Show(ex.Message, "Can't open image!"); return; } Bitmap bmp = ((ImageForm)this.ActiveMdiChild).MainImage; BitmapData bmData = null; int bytesPerPixel, heightInPixels, widthInPixels; StreamWriter writer = new StreamWriter(Path.GetFullPath(saveFileDialog1.FileName), false); try { bmp.LockImage(out bmData, out bytesPerPixel, out heightInPixels, out widthInPixels); unsafe { for (int y = 0; y < heightInPixels; y++) { for (int x = 0; x < widthInPixels; x += bytesPerPixel) { writer.Write(bmData.GetPixelXY(x, y)); if (x != widthInPixels - 1) { writer.Write(';'); } } if (y != heightInPixels - 1) { writer.WriteLine(); } } } bmp.UnlockBits(bmData); writer.Dispose(); } catch { try { bmp.UnlockBits(bmData); writer.Dispose(); } catch { } } } }