public static string Bitmap2string(BitmapSource img) { /* 转换图片为灰度图并拆分为数组 */ var gray8_img = new Gray8Image(img); var pixels = gray8_img.ImagePixels; var info = gray8_img.Info; /* 转换图片像素数组转化为字符画 */ /* 如果 buffer 长度不够,将不会执行转换 */ var length = (uint)((info.Width + 2) * (info.Height >> 1)); var buffer = new byte[length]; ImgFunc.img2txt(pixels, (uint)info.Width, (uint)info.Height, buffer, ref length); /* * [How to convert byte[] to string?](http://stackoverflow.com/questions/1003275/how-to-convert-byte-to-string) */ var ret = System.Text.Encoding.UTF8.GetString(buffer, 0, (int)length); return(ret); }
public Gray8Image(Gray8Image rhs) { if (rhs == this) return; this.imgInfo = rhs.imgInfo; this.imgPixels = rhs.imgPixels.Clone() as byte[]; }
private void LoadImg(BitmapSource img) { /* * 笔记: * * 最开始尝试使用 System.drawing 实现转换为 byte,想了想还是算了(各种转化),这些是当时的资料 * 参考资料: * [Convert a bitmap into a byte array](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array?lq=1) * [System.drawing namespace not found under console application](http://stackoverflow.com/questions/8553136/system-drawing-namespace-not-found-under-console-application) * * 最后决定还是直接转换 BitmapImage 到 byte[] * * 然后翻了一下这个 [BitmapImage to byte[]](http://stackoverflow.com/questions/6597676/bitmapimage-to-byte) * (虽然最后并没有用上,但是知道了如何转换 BitmapImage 的格式) * * 最后,参考资料: * [Finding specific pixel colors of a BitmapImage](http://stackoverflow.com/questions/1176910/finding-specific-pixel-colors-of-a-bitmapimage) * 这里强行把图片换成了 PixelFormats.Bgra32,同时使用 CopyPixels 获取各个像素信息到 byte[] 中, * 这样就可以交给外部方法处理了。 */ /* 保存 Bgra32 版本的像素 */ bgraImgSrc = new Bgra32Image(img); //BgraImgNow = new Bgra32Image(BgraImgSrc); /* 保存 8bit 灰度图的版本 */ gray8ImgSrc = new Gray8Image(img); /* 根据图片大小,确定是否实时计算 */ realtimeProcessing = (img.PixelWidth * img.PixelHeight) < (imgWidMax * imgHgtMax); /* 确定采样的上限值 */ sliderSampling.Maximum = Math.Max(img.PixelWidth, img.PixelHeight); /* 调整窗口大小 */ ResizeForImg(this.imgSrc); /* 显示原图像 */ OriginImg(this.imgSrc); /* 启用 保存 按钮 */ try { if (ImgFunc.is_loaded()) { EnbaleComponent(); } /* 刷新直方图 */ UpdateHistogram(); } catch (Exception e) { MessageBox.Show( e.Message, "加载 ImgFuncLib 时发生错误", MessageBoxButton.OK, MessageBoxImage.Error ); } }
public Gray8Image(Gray8Image rhs) { if (rhs == this) { return; } this.imgInfo = rhs.imgInfo; this.imgPixels = rhs.imgPixels.Clone() as byte[]; }
private void LoadImg(BitmapSource img) { /* * 笔记: * * 最开始尝试使用 System.drawing 实现转换为 byte,想了想还是算了(各种转化),这些是当时的资料 * 参考资料: * [Convert a bitmap into a byte array](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array?lq=1) * [System.drawing namespace not found under console application](http://stackoverflow.com/questions/8553136/system-drawing-namespace-not-found-under-console-application) * * 最后决定还是直接转换 BitmapImage 到 byte[] * * 然后翻了一下这个 [BitmapImage to byte[]](http://stackoverflow.com/questions/6597676/bitmapimage-to-byte) * (虽然最后并没有用上,但是知道了如何转换 BitmapImage 的格式) * * 最后,参考资料: * [Finding specific pixel colors of a BitmapImage](http://stackoverflow.com/questions/1176910/finding-specific-pixel-colors-of-a-bitmapimage) * 这里强行把图片换成了 PixelFormats.Bgra32,同时使用 CopyPixels 获取各个像素信息到 byte[] 中, * 这样就可以交给外部方法处理了。 */ /* 保存 Bgra32 版本的像素 */ bgraImgSrc = new Bgra32Image(img); //BgraImgNow = new Bgra32Image(BgraImgSrc); /* 保存 8bit 灰度图的版本 */ gray8ImgSrc = new Gray8Image(img); /* 根据图片大小,确定是否实时计算 */ realtimeProcessing = (img.PixelWidth * img.PixelHeight) < (imgWidMax * imgHgtMax); /* 确定采样的上限值 */ sliderSampling.Maximum = Math.Max(img.PixelWidth, img.PixelHeight); /* 调整窗口大小 */ ResizeForImg(this.imgSrc); /* 显示原图像 */ OriginImg(this.imgSrc); /* 启用 保存 按钮 */ try { if(ImgFunc.is_loaded()) EnbaleComponent(); /* 刷新直方图 */ UpdateHistogram(); } catch(Exception e) { MessageBox.Show( e.Message, "加载 ImgFuncLib 时发生错误", MessageBoxButton.OK, MessageBoxImage.Error ); } }