Tag: bitmap

如何从C#中的绘制面板获取位图

我有一个面板,我使用它的Graphics gr = panel1.CreateGraphics()来绘制线条和其他东西。 我需要获得点击鼠标的点的像素颜色,所以我决定使用Bitmap的GetPixel方法。 我这样创建位图: Bitmap b = new Bitmap(width, height); panel1.DrawToBitmap(b, new Rectangle(0, 0, width, height)); b.Save(“D:/aaa.bmp”); 但即使我画了任何东西,我也只得到白色矩形。 有什么问题?

增加图像的分辨率(dpi)

我不确定没有一些第三方库会有多可行,但是这里有: 我有一个450×900的图像,我试图打印。 问题是,我用来打印的方法是将原始数据发送到打印机。 图像分辨率为96dpix96dpi,打印机运行速度为203dpi。 所以……图像很小。 我需要增加图像的dpi,以“真实”的尺寸打印它。 Bitmap b0 = LoadBitmap(); //I need to rotate it because for some odd reason it prints backwards and upside down. b0.RotateFlip(RotateFlipType.Rotate180FlipX); //Set a new resolution, 203dpi b0.SetResolution(203, 203); //I need to save and reload the bitmap, because RotateFlip compresses it. //(annoying as hell, took me ages to figure out […]

c#将非常大的位图保存为jpegs(或任何其他压缩格式)

我目前正在处理非常大的图像,这些图像基本上是通过拼接许多较小的图像(例如全景图或照片马赛克软件)生成的。 为了避免内存不足exception(在内存中只是如何排列较小图像的“地图”),我写了一些代码,使用BinaryWriter和LockBits逐行保存这些图像作为位图。 到现在为止还挺好。 现在问题是我想将这些图像保存为Jpegs(或PNG)。 由于我对c#很陌生,我现在只能想到两种方法: 1)类似于位图保存程序。 生成一些jpeg标题并逐行保存大图像,以前以某种方式压缩它们。 我不知道如何执行压缩。 2)将已保存的位图流式传输到内存中并将其保存为编码的jpeg。 由于第二种方法似乎更容易,我尝试过这样的事情: FileStream fsr = new FileStream(“input.bmp”, FileMode.Open, FileAccess.Read); FileStream fsw = new FileStream(“output.jpg”, FileMode.CreateNew, FileAccess.Write); EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L); Bitmap bmp = new Bitmap(fsr); bmp.Save(fsw, GetEncoder(ImageFormat.Jpeg), encoderParameters); bmp.Dispose(); 现在的问题是save-method尝试首先将位图完全加载到内存中,从而导致内存不足exception。 如果有任何关于如何解决或避免这个问题的建议,我会非常高兴!

myBitmap.RawFormat与任何已知的ImageFormat有什么不同?

我正在使用GDI +,我创建了一个像这样的新位图: var bmp = new Bitmap(width, height); 现在当我观察它的RawFormat.Guid时,我发现它与所有预定义的ImageFormats不同(我希望它是JPEG)。 ImageFormat.Jpeg.Guid {b96b3cae-0728-11d3-9d7b-0000f81ef32e} format.Guid {b96b3caa-0728-11d3-9d7b-0000f81ef32e} 有趣的是,你可以看到它们是相同的,除了一个让我更加困惑的角色。 知道为什么吗? 哪些代码部分决定了我创建的位图的RawFormat是什么? 我们如何确保它是一个有效的ImageFormat? 谢谢。

.NET使用与加载时相同的质量保存jpeg

我有一个大炮数码相机,我设置它拍摄超细质量的照片,它输出一个3兆的.jpg文件。 如果我在ASP.NET中加载它(这有助于更改它的dpi分辨率或裁剪它或whaterver) imgPicture = Image.FromFile(Config.WorkDirectory + this.TempPhotoName); bmpPicture = new Bitmap(imgPicture); 然后我再次保存它: bmpModified.Save(Config.WorkDirectory + this.TempPhotoName,System.Drawing.Imaging.ImageFormat.Jpeg); 它输出的jpg大小只有700KB左右。 质量下降。 我也试过保存它: bmpPicture.Save(Config.WorkDirectory + this.TempPhotoName, codecJpeg, encparams); codecJpeg是哪里的 ImageCodecInfo codecJpeg = this.getEncoderInfo(“image/jpeg”); private ImageCodecInfo getEncoderInfo(string mimeType) { // Get image codecs for all image formats ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); // Find the correct image codec for (int i = […]

在图片框中绘制列车时,c#中的内存不足exception

我正在尝试创建一个显示图片picturebox在线列车的应用程序 因此,要实现这一点,我创建一个worker thread来获得在线列车位置。所以我定义了线程,你可以在这里看到: private Thread workerThread = null; private delegate void UpdateListBoxDelegate(); private UpdateListBoxDelegate UpdateListBox = null; 在Form_load我称之为: UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus); // Initialise and start worker thread workerThread = new Thread(new ThreadStart(this.GetOnlineTrain)); workerThread.Start(); 我委托处理的方法是: private void UpdateStatus() { foreach (TimeTable onlineTrain in OnlineTrainList.ToList()) { if (lstSensorLeft.Count != 0 || lstSensorRight.Count != 0) { pictureBoxonlineTrain.Image […]

Tesseract OCR引擎无法从自动生成的图像中读取文本,但可以从MS Paint中的CUT读取

我正在为Tesseract OCR引擎使用.NET包装器。 我有一个大型文档是PNG。 当我在MS油漆中切出一段图像然后将其送入引擎时,它可以工作。 但是当我在代码中执行此操作时,引擎无法识别图像中的文本。 图像看起来相同,并且属性不会显得非常偏离。 所以我有点困惑。 这是两张图片。 来自MS涂料: 来自代码: 这是我从MS油漆图像中得到的: 并通过代码: 他们真的很相似所以我不确定为什么它不能识别第二个文本。 以下是我如何生成图像。 public Bitmap CropImage(Bitmap source, Rectangle section) { Bitmap bmp = new Bitmap(section.Width, section.Height); Graphics g = Graphics.FromImage(bmp); g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel); return bmp; } private void Form1_Load(object sender, EventArgs e) { Bitmap source = new Bitmap(test); Rectangle section = new […]

大的,每像素1位的位图导致OutOfMemoryException

我想做的是从磁盘加载图像并从中创建一个BitmapSource 。 图像为14043px x 9933px,为b / w(1bpp)。 但是我遇到了OutOfMemoryException因为以下代码消耗大约800 MB RAM。 以下代码在我的特定文件的维度中生成ImageSource 。 我这样做是为了看看我是否可以在不使用磁盘上的实际文件的情况下使其工作。 public System.Windows.Media.ImageSource getImageSource(){ int width = 14043; int height = 9933; List colors = new List(); colors.Add(System.Windows.Media.Colors.Black); colors.Add(System.Windows.Media.Colors.White); BitmapPalette palette = new BitmapPalette(colors); System.Windows.Media.PixelFormat pf = System.Windows.Media.PixelFormats.Indexed1; int stride = width / pf.BitsPerPixel; byte[] pixels = new byte[height * stride]; for (int […]

使用C#从BitmapData裁剪区域

我有一个位图sourceImage.bmp 锁定它的位: BitmapData dataOriginal = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 做分析, 得到一个克隆 : Bitmap originalClone = AForge.Imaging.Image.Clone(dataOriginal); 解锁位: sourceImage.UnlockBits(dataOriginal); 是否可以指定要复制的“dataOriginal”的哪一部分(x,y,w,h)? 或者从dataOriginal创建新数据,指定X和Y坐标以及H和W? 目的是从该图像中复制一个小区域。 这个方法可能比DrawImage更快,这就是我不使用后者的原因。 编辑: 所以我拿了29 Mb位图并进行了一些硬核测试! 全尺寸裁剪(基本上是副本)+ 100次迭代。 码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AForge; using AForge.Imaging; using System.Diagnostics; using System.Drawing.Imaging; […]

如何在.NET中使用大位图?

我正在尝试编写轻量级图像查看应用程序。 但是,.NET存在系统内存限制。 当尝试加载大位图( 9000 x 9000 px或更大,24位)时,我得到一个System.OutOfMemoryException。 这是在具有2GB RAM(其中1.3GB用完)的Windows 2000 PC上。 尝试加载文件也需要很长时间。 以下代码生成此错误: Image image = new Bitmap(filename); using (Graphics gfx = this.CreateGraphics()) { gfx.DrawImage(image, new Point(0, 0)); } 这段代码也是如此: Stream stream = (Stream)File.OpenRead(filename); Image image = Image.FromStream(stream, false, false); using (Graphics gfx = this.CreateGraphics()) { gfx.DrawImage(image, new Rectangle(0, 0, 100, 100), 4000, 4000, 100, […]