目前,我正在使用该SetPixel()方法来更改位图中每个像素的颜色。这对于小尺寸的小图像效果很好,但是当我在大图像上测试它时,它确实需要一段时间。
我以前没有在 VB.Net 中处理过图像,所以我可能只是忽略了一些明显的东西。我这样做是为了制作一个将图像转换为灰度的程序。这会产生正确的结果,但速度较低,并且在此期间 UI 会冻结,因此我热衷于最大限度地提高转换速度。
这是我目前的代码:
Dim tmpImg As New Bitmap(img) '"img" is a reference to the original image
For x As Integer = 0 To tmpImg.Width - 1
For y As Integer = 0 To tmpImg.Height - 1
Dim clr As Byte
With tmpImg.GetPixel(x, y)
clr = ConvertToGrey(.R, .G, .B)
End With
tmpImg.SetPixel(x, y, Color.FromArgb(clr, clr, clr))
Next
Next
Private Function ConvertToGrey(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As Byte
Return …Run Code Online (Sandbox Code Playgroud) 我想清除位图缓存,但我不知道如何清除位图。我正在使用 Koush Ion 库来加载图片
try {
bitmap = Ion.with(context).load(URLimage).withBitmap().asBitmap().get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
每次单击按钮时我都需要清除位图缓存。提前致谢
我正在使用依赖于 Google 的 Grafika 存储库的实时流 API。我使用 Grafika EGLSurfaceBase 的 saveFrame 方法来允许用户在流式传输时捕获视频的静态图像。
https://github.com/google/grafika/blob/master/src/com/android/grafika/gles/EglSurfaceBase.java
实际捕获有效,但显然在某些相机方向上图像会翻转。
我发现了很多与从 OpenGL 纹理获取的倒置位图相关的问题 - 但大多数似乎是指绘制的图像并依赖于:
a) 在 OpenG 中翻转纹理。但就我而言,我正在使用实时流 API,因此翻转纹理来捕获图像实际上可能也会翻转视频流上的图像捕获。
或者
b) 在基于资源生成位图后翻转位图。就我而言,我没有资源,我正在从字节缓冲区创建位图,并且不想复制它来翻转它。
这是 API 具有的基本 EGLSurfaceBase 方法 - 我将把相机方向传递给它,但我的问题是:
String filename = file.toString();
int width = getWidth();
int height = getHeight();
ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
buf.order(ByteOrder.LITTLE_ENDIAN);
GLES20.glReadPixels(0, 0, width, height,
GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
GlUtil.checkGlError("glReadPixels");
buf.rewind();
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(filename));
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); …Run Code Online (Sandbox Code Playgroud) 我这里有一个示例 wpf 应用程序,我想知道为什么 BMP 加载速度比 PNG 快。这是确切的设置:
- Windows 7
- Visual Studio 2013
- landscape.png, 1920x1080, 2.4mb
- landscape.bmp, 1920x1080, 5.6mb
- wpf-app, class MainWindow.xaml.cs constructor, no other code before
Run Code Online (Sandbox Code Playgroud)
代码:
var sw = Stopwatch.StartNew();
var Bitmap1 = new Bitmap("landscape.bmp");
long t1 = sw.ElapsedMilliseconds;
sw.Restart();
var Bitmap2 = new Bitmap("landscape.png");
long t2 = sw.ElapsedMilliseconds;
Run Code Online (Sandbox Code Playgroud)
因此 BMP 加载大约需要 6 毫秒,PNG 需要 40 毫秒。
为什么会这样?
我正在尝试截取整个屏幕的屏幕截图,并希望通过网络将其发送到服务器。(服务器将保存它)
我目前正在使用这段代码
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);
CImage image;
image.Attach(hBitmap);
image.Save(L"screen.jpg");
Run Code Online (Sandbox Code Playgroud)
它确实保存了文件,但这不是我想要的。我也不确定如何获取字节,我正在考虑,GetBits()但文档说它是指向底层位图数据的指针。
我想要得到的是 JPG 数据,我可以通过网络发送该数据并使用服务器保存图像。
那么,为什么我不直接使用位图并直接发送呢?因为它有点复杂并且代码很长,所以您必须获取标头和其他内容。但我可能是错的。
我的问题:
- 如何从图像中获取字节以便将其发送到服务器?(就像保存到数据库的 BLOB)
- 有任何库可以帮助我转换并获取内存中的所有字节吗?
references:
https://msdn.microsoft.com/en-us/library/ms535406(v=VS.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms535406(v=vs.85).aspx
http://www.codeproject.com/Tips/738533/save-load-image-between-buffer
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试构建 16 位灰度“渐变”图像,但我的输出看起来很奇怪,所以我显然没有正确理解这一点。我希望有人能够对我的问题提供一些知识。我认为我写的“位图”是错误的?但我不确定。
#include "CImg.h"
using namespace std;
unsigned short buffer[1250][1250];
void fill_buffer()
{
unsigned short temp_data = 0;
for (int i =0;i < 1250; i++)
{
for (int j =0 ;j < 1250;j++)
{
buffer[i][j] = temp_data;
}
temp_data += 20;
}
}
int main()
{
fill_buffer();
auto hold_arr = (uint8_t *)&buffer[0][0];
cimg_library::CImg<uint8_t> img(hold_arr, 1250, 1250);
img.save_bmp("test.bmp");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
电流输出:

我有一个 8bpp 索引位图,带有自定义 256 色调色板,其中调色板中的特定颜色 (Color.Pink和Color.Green) 表示透明度。
我可以MakeTransparent(color)在位图上使用该方法(每种颜色两次),但它会将其转换为 32bpp。所以我使用的是:
using var imageAttr = new ImageAttributes();
imageAttr.SetColorKey(pink, pink, ColorAdjustType.Default);
Run Code Online (Sandbox Code Playgroud)
进而
g.DrawImage(bitmap, destRect, X, Y, Width, Height, GraphicsUnit.Pixel, imageAttr);
Run Code Online (Sandbox Code Playgroud)
它按应有的方式绘制位图,但仅对Color.Pink透明颜色进行更改。我怎样才能对第二种颜色(Color.Green)也执行此操作?
我正在尝试使用 CameraX 图像分析(版本 1.1.0-alpha08)的这个新功能:使用 setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888),发送到分析仪的图像将具有 RGBA 格式。
我需要将发送到分析器的图像转换为位图,以便我可以将其输入到 TensorFlow 分类器。如果没有这个新功能,我将收到标准 YUV_420_888 格式的图像,然后我将不得不使用可以在 google 上搜索到的几种解决方案之一,以便将 YUV_420_888 转换为 RGBA,然后转换为位图。像这样: https: //blog.minhazav.dev/how-to-convert-yuv-420-sp-android.media.Image-to-Bitmap-or-jpeg/。我认为直接以 RGBA 格式获取媒体图像应该可以帮助我避免实现那些痛苦的解决方案(我实际上已经尝试过,但到目前为止似乎对我来说效果不太好)。
问题是我不知道如何将此 RGBA 媒体图像转换为位图。我注意到调用 mediaImage.getFormat() 返回 1 ,它不是 ImageFormat 值,而是 PixelFormat 值,逻辑上对应于 RGBA_8888 格式,这与文档一致:“所有 ImageProxy 发送到 ImageAnalysis.Analyzer.analyze( ImageProxy) 的格式为 PixelFormat.RGBA_8888"。
我已经尝试过这个:
private Bitmap toBitmapRGBA(Image image) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
buffer.rewind();
int size = buffer.remaining();
byte[] bytes = new byte[size];
buffer.get(bytes);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
return bitmapImage;
}
Run Code Online (Sandbox Code Playgroud)
这将返回 null …
我试图保存具有指定编码质量的位图jpg格式.但是,在调用save方法时,我得到一个异常("参数无效.").
如果我省略了bmp.save中的最后两个参数,它可以正常工作.
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 16);
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
string outfile = outputpath + "\\" + fileaddition + sourcefile.Name;
bmp.Save(outfile,ici,eps );
bmp.Dispose();
image.Dispose();
return true;
}
ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我不知道一个更好的头衔,但我会描述这个问题.
我们使用的硬件具有显示图像的能力.它可以显示分辨率为64 x 256的黑白图像.
问题是我们必须发送到设备的图像格式.它不是标准的位图格式,而是一个代表图像每个像素的字节数组.
0 =黑色,1 =白色.
因此,如果我们有一个大小为4 x 4的图像,则字节数组可能如下所示:
1000 0100 0010 0001
图像看起来像:
位图http://www.mediafire.com/imgbnc.php/6ee6a28148d0170708cb10ec7ce6512e4g.jpg
问题是我们需要通过在C#中创建单色位图来创建此图像,然后将其转换为设备可以理解的文件格式.
例如,可以在设备上显示文本.为此,他必须创建一个位图并向其写入文本:
var bitmap = new Bitmap(256, 64);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.DrawString("Hello World", new Font("Courier", 10, FontStyle.Regular), new SolidBrush(Color.White), 1, 1);
}
Run Code Online (Sandbox Code Playgroud)
这里有两个问题:
所以我需要一种方法:
我发现你可以将像素深度设置为16,24或32位,但没有找到单色,我不知道如何读取像素数据.
欢迎提出建议.
更新:我不能使用Win32 PInvokes ...必须是平台中立的!
关注:以下代码现在适用于我.(以防任何人需要)
private static byte[] GetLedBytes(Bitmap bitmap)
{
int threshold = 127;
int index = 0;
int dimensions = bitmap.Height * bitmap.Width;
BitArray bits = new …Run Code Online (Sandbox Code Playgroud)