Tag: 位图

位图步幅和4字节关系?

这句话的意思是: Stride属性,以字节为单位保存一行的宽度。 然而,行的大小可能不是像素大小的精确倍数,因为为了提高效率,系统确保将数据打包成以四字节边界开始并填充为四个字节的倍数的行。

使用不安全代码的C#位图

我正在使用以下代码在C#中制作图像蒙版: for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { bmp.SetPixel(x,y,Color.White); } } for(int x = left; x < width; x++) { for(int y = top; y < height; y++) { bmp.SetPixel(x,y,Color.Transparent); } } 但这太慢了……与此相比,不安全的是什么? 它会分配得更快吗? 最后我用PNG格式做了一个bmp.Save()。 更新: 按照MusiGenesis的建议阅读http://sofzh.miximages.com/c%23/Alpha 0-255 } } } bmp.UnlockBits(bmd); bmp.Save(test.png”,ImageFormat.Png); Alpha通道:0表示完全透明,255表示该像素没有透明度。 […]

更快的位图对比度算法

我有一个带有轨迹栏滑块控件的工具,用于调整图像的亮度,对比度,伽玛等。 当用户拖动滑块时,我试图获得我的图像的实时更新。 亮度和伽玛算法是可接受的速度(大约170ms)。 但对比度算法大约是380ms。 基本上我的表单是一个带滑块的工具窗口。 每次更新图像时,它都会向父项发送一个事件,重新绘制新图像。 工具窗口将原始未修改的图像锁定在内存中,因此我始终可以访问它的字节。 因此,每次更改滑块的ValueChanged事件(例如“对比度”滑块)时,我都会这样做。 工作(目标)位图的LockBits为Format24bppRgb(原始位图在Format32bppPArgb中) Marshal。将位复制到byte []数组 检查我正在做的操作(选择了哪个滑块) 对比度使用以下代码: 码: double newValue = 0; double c = (100.0 + contrast) / 100.0; c *= c; for (int i = 0; i < sourcePixels.Length; i++) { newValue = sourcePixels[i]; newValue /= 255.0; newValue -= 0.5; newValue *= c; newValue += 0.5; newValue […]

有没有从XNA中的Bitmap对象创建Texture2D的快速替代方法?

我已经环顾了很多,我发现从Bitmap创建Texture2D的唯一方法是: using (MemoryStream s = new MemoryStream()) { bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png); s.Seek(0, SeekOrigin.Begin); Texture2D tx = Texture2D.FromFile(device, s); } 和 Texture2D tx = new Texture2D(device, bmp.Width, bmp.Height, 0, TextureUsage.None, SurfaceFormat.Color); tx.SetData(rgbValues, 0, rgbValues.Length, SetDataOptions.NoOverwrite); 其中rgbValues是一个字节数组,包含32位ARGB格式的位图像素数据。 我的问题是,我可以尝试更快的方法吗? 我正在编写一个地图编辑器,它必须读取自定义格式图像(地图图块)并将它们转换为Texture2D纹理才能显示。 以前版本的编辑器是一个C ++实现,它首先将图像转换为位图,然后转换为使用DirectX绘制的纹理。 我在这里尝试了相同的方法,但是上述两种方法都非常慢。 要加载到内存中,映射所需的所有纹理都需要第一个方法~250秒,对于合理的规格计算机,第二个方法需要~110秒(为了比较,C ++代码大约需要5秒)。 如果有一种方法可以直接编辑纹理数据(例如使用Bitmap类的LockBits方法),那么我就可以将自定义格式图像直接转换为Texture2D,并希望节省处理时间。 任何帮助将非常感谢。 谢谢

获取图像中每种颜色的百分比使用情况

我有这个工作,但它在jpeg图像上非常缓慢,也需要一些改变。 我需要知道图像中的各个颜色(RGB的公差为+/- 1)和该颜色的图像的百分比。 因此,如果图像是黑白的,则会出现白色:74%黑色:26% 下面的代码就像我说的那样,但我需要添加一个容差系统,我不知道如何做到这一点。 private Dictionary getPixelData(Bitmap image) { Dictionary pixelData = new Dictionary(); //int col, row; //int r, g, b; Color pixel; double offset = 0.000001; int hmm = (image.Height * image.Width); double current = 0; offset = 100 / double.Parse(hmm.ToString());// 0.01;// 100 / (image.Height * image.Width) * 10000; try { for (int […]

位图byte 的大小与BMP内存流大小不同

我试图通过tcp / ip连接发送位图。 到目前为止,我的程序工作正常。 但在调试过程中,我发现了我的位图byte []的奇怪值。 我打开一个24位位图并将其转换为16位。 位图是800×600所以字节[]长度应该是800 * 800 * 2Byte = 960000Byte …但我的数组是960054 … 额外的字节来自哪里? Console.WriteLine(“Bitmap auf 16Bit anpassen…\n”); Rectangle r = new Rectangle(0,0,bitmap_o.Width, bitmap_o.Height); Bitmap bitmap_n = bitmap_o.Clone(r, PixelFormat.Format16bppRgb555); bitmap_n.Save(“test2.bmp”); Console.WriteLine(“Neue Bitmap-Eigenschaften:”); Console.WriteLine(bitmap_n.Width.ToString()); Console.WriteLine(bitmap_n.Height.ToString()); Console.WriteLine(bitmap_n.PixelFormat.ToString()); byte[] data = new byte[0]; MemoryStream mem_stream = new MemoryStream(); bitmap_n.Save(mem_stream, ImageFormat.Bmp); data = mem_stream.ToArray(); mem_stream.Close(); Console.WriteLine(data.Length.ToString()); […]

删除位图的黑色背景颜色

我需要在C#VS2013中删除位图的黑色背景颜色。 就像我在canvas上画一些点一样。 canvas是黑色的,我只需要将canvas更改为透明,同时在其上保留彩色点而不做任何更改。 我得到了解决方案: 如何从Bitmap中删除白色背景颜色 Bitmap capcha = new Bitmap(@”C:/image.png”); capcha.MakeTransparent(Color.Black); 但是,背景仍有灰色,如覆盖图像上的点的雾。 如何删除它? 更新我使用的代码: ImageAttribute imageAttribute = new ImageAttribute(); imageAttribute.SetGamma(0.5f, ColorAdjustType.Bitmap); gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttribute ); 我有同样的事情。 更多C#代码更新以绘制图像: System.Drawing.Bitmap canvasImage = new System.Drawing.Bitmap(xSize, ySize, System.Drawing.Imaging.PixelFormat.Format32bppArgb); canvasImage.MakeTransparent(Color.Black); Graphics g = Graphics.FromImage(canvasImage); System.Drawing.Bitmap tempImage = myDrawImageFunction(myPoints); g.Clear(Color.Transparent); // this […]

G.drawimage绘制的大小不正确

我正在创建一个包含文件中较小图像的大位图。 这些图像的大小都是250像素,但其中一个变小,另一个大于250像素。 我只是使用基本的g.drawimage方法,所以我不知道我做错了什么。 string[] imagePaths = Directory.GetFiles(@”C:\Users\admin\Desktop\Images”); ArrayList images = new ArrayList(); foreach (var item in imagePaths) images.Add(new Bitmap(item, true)); Bitmap list = new Bitmap(840, 1188); Graphics g = Graphics.FromImage(list); int size = 0; for (int i = 0; i < images.Count; i++) { g.DrawImage((Bitmap)images[i], new Point(10, (i + 1) * 10 + size)); Bitmap […]

实现IDisposable C#

我在理解IDisposable界面时遇到了一些麻烦 我正在开发一个游戏引擎,并在我的解决方案上运行代码分析,并被告知在我的“GrapicsEngine”类上实现IDisposable接口,因为它包含一个Bitmap实例。 当然,我搜索了互联网,以了解如何正确地做到这一点,并提出了以下代码: 这是我的class级成员: private const int BITMAP_WIDTH = 4096; private const int BITMAP_HEIGHT = 4096; private Graphics backBuffer; private Bitmap bitmap; private Color clearColor; private WindowSurface surface; private GraphicsQuality quality; private Viewport viewport; 这是我的IDispoable地区: #region IDisposable public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing == true) ReleaseManagedResources(); […]

如何将透明PNG加载到位图并忽略Alpha通道

我看到很多关于如何加载带有alpha通道的PNG并显示它的问题,但没有关于如何加载具有alpha通道但忽略alpha通道的PNG,揭示底层RGB数据。 我试过简单地删除alpha通道中的所有数据,但我只是得到黑色像素而不是我想要的颜色数据。 从我创建Bitmap对象的那一刻起,它似乎不包含alpha为0的像素中的任何颜色信息,尽管我向你保证PNG中的信息实际存在。 要清楚,如果我的图像包含具有这些值的像素:(R:255 G:128 B:128 A:0)我需要该像素(R:255 G:128 B:128 A:255),而不是(R:0 G:0 B:0 A:255)。 谢谢 编辑:以前的信息不正确。 位图包含使用时的所有信息: Bitmap myImage = Bitmap(“filename.png”) ; 我做错了是后来使用: Bitmap otherImage = Bitmap(myImage); 似乎这就是我需要的数据无效。 相反,我现在正在使用 Bitmap otherImage = (Bitmap)(myImage.Clone()); 然后使用lockbits手动将alpha通道设置为不透明。 我希望这对未来的某些人有用。