我需要捕获给定窗口HWND句柄的窗口屏幕并将捕获结果存储在ID2D1Bitmap对象中,以便通过渲染目标绘制此位图。
我怎样才能达到这个结果?
我有一个 SVG 字节数组,需要将其转换为 Android 中的图像。字节数组使用 Base64 进行编码。我相信 Android 不支持 SVG 图像,这导致我的转换方法返回 null。无论如何,这可以做到吗?
这是我尝试用来转换图像的方法:
public static Bitmap imageFromString(String imageData) {
String data = imageData.substring(imageData.indexOf(",") + 1);
byte[] imageAsBytes = Base64.decode(data.getBytes(), Base64.DEFAULT);
return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
}
Run Code Online (Sandbox Code Playgroud) 我在使用 JpegBitmapEncoder 时遇到困难,因为它正在创建放置在黑色矩形中的图像。而且我没有修复的解决方案。
private void SaveImage(Canvas canvas, string fileName)
{
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Pic";
s.DefaultExt = ".jpg";
s.Filter = "JPG files (.jpg)|*.jpg";
Nullable<bool> result = s.ShowDialog();
if (result == true)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(6646, 3940, 2000d, 2000d, PixelFormats.Pbgra32);
canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
renderBitmap.Render(canvas);
string filename = s.FileName;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
using (FileStream file = File.Create(filename))
{
encoder.Save(file);
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这段代码我得到:
但是当我使用 PngBitmap Encoder 时,这种情况不会发生。任何人都可以发光吗?如何删除黑色矩形或通过增加图片的尺寸来填充它?
我正在尝试压缩用相机拍摄的照片而不缩放图像尺寸。
我尝试了 bitmap.compress,但没有成功。只是为了确保我尝试压缩从资源加载的位图,但仍然没有成功。
有任何想法吗?
示例代码:
//load bitmap from resources:
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.trolltunga);
Log.d("roee", "onImageCaptured: before compress = " + bitmap.getByteCount() + ", " + bitmap.getWidth() + ", " + bitmap.getHeight());
//compressing
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
Bitmap comp = BitmapFactory.decodeStream(new ByteArrayInputStream(bos.toByteArray()));
Log.d("roee", "onImageCaptured: after compress = " + comp.getByteCount() + ", " + comp.getWidth() + ", " + comp.getHeight());
Run Code Online (Sandbox Code Playgroud)
记录结果:
D/roee: onImageCaptured: before compress = 10800000, 3000, 900
D/roee: onImageCaptured: after compress = 10800000, 3000, 900
Run Code Online (Sandbox Code Playgroud) Bitmap.LockBits 中 ImageLockMode 的用途是什么?\n对于 ReadOnly,文档仅说明
\n\n\n\n\nReadOnly:指定图像的一部分被锁定以供读取。
\n
但下面的代码证明,这不是真的。\n我知道这个问题之前已经被问过,这次我尝试使用一些实际的代码,因为我在其他地方找不到答案。
\n\n如果我运行以下代码,它的行为与答案中所解释的完全一样。
\n\nusing System;\nusing System.Drawing;\nusing System.Drawing.Imaging;\nusing System.Runtime.InteropServices;\n\nnamespace LockBits_Trials\n{\n class Program\n {\n static readonly Random rnd = new Random(42);\n static void Main(string[] args)\n {\n Bitmap bmp_fromFile = new Bitmap("example.png");\n Bitmap bmp_fromCtor = new Bitmap(100, 100, PixelFormat.Format24bppRgb);\n marshalCopy(bmp_fromFile, "result_marshalCopy_fromFile.png");\n marshalCopy(bmp_fromCtor, "result_marshalCopy_fromCtor.png");\n usePointer(bmp_fromFile, "result_usePointer_fromFile.png");\n usePointer(bmp_fromCtor, "result_usePointer_fromCtor.png");\n }\n\n private static unsafe void usePointer(Bitmap bmp, string filename)\n {\n ImageLockMode mode = ImageLockMode.ReadOnly;\n //code from turgay at http://csharpexamples.com/fast-image-processing-c/ \n if (bmp.PixelFormat != …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ARCore 从 ARSession 的当前帧获取位图。但它始终等于 null。我已经在网上搜索了一段时间,但无法弄清楚我做错了什么。
try {
capturedImage = mFrame.acquireCameraImage();
ByteBuffer buffer = capturedImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,null);
if (bitmap == null)
Log.e(TAG,"Bitmap was NOT initialized!");
} catch(Exception e){
}
Run Code Online (Sandbox Code Playgroud)
我正在mFrame从onDrawFrame我GLSurfaceView用来显示相机图像的地方获取信息。一切工作正常,除了我的位图等于空。
我使用的是按钮,因此只使用了一个框架,如下所示:
scanButton = (Button) findViewById(R.id.scanButton);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkbox = false;
if (capturedImage!=null) capturedImage.close();
BitmapMethod();
}
});
Run Code Online (Sandbox Code Playgroud)
capturedImage,buffer并且bytes全部不等于 null。
可能有什么问题吗mFrame.acquireCameraImage()?
多谢
我使用下面的代码将 SVG 字符串 (svgString) 转换为位图 (myBitmap)。理想情况下,我想避免往返硬盘。你认为这可能吗(没有找到太多 SVG nuget 包的文档)。顺便说一句,我知道我也可以将 bmp 写入硬盘,但我选择了 png,因为由于某种原因,bmp 的背景始终是黑色的。
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Svg;
namespace Bla
{
class Program
{
static void Main(string[] args)
{
var svgPath = @"d:\test.svg";
var pngPath = @"d:\test.png";
var svgString = @"
<!DOCTYPE html>
<html>
<body>
<svg height='200' width='500'>
<polyline points='20,20 40,25 60,40 80,120 120,140 200,180' style='fill:none;stroke:black;stroke-width:3' />
Sorry, your browser does not support inline SVG.
</svg>
</body>
</html>
";
File.WriteAllText(svgPath, svgString);
var svgDocument = SvgDocument.Open(svgPath);
svgDocument.ShapeRendering = …Run Code Online (Sandbox Code Playgroud) 我很高兴将位图保存为 PNG 或 JPG(两者都不起作用),但似乎使用内容值无法按预期工作。
我缺少什么?适用于 Android 10,但不适用于 Android 8
fun Bitmap.save(context: Context) {
val contentResolver = context.contentResolver
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, "test.png")
put(MediaStore.MediaColumns.TITLE, "test")
put(MediaStore.MediaColumns.MIME_TYPE, "image/png")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
put(MediaStore.MediaColumns.IS_PENDING, 1)
}
}
val contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val uri = contentResolver.insert(contentUri, contentValues)
if (uri != null) {
try {
contentResolver.openFileDescriptor(uri, "w", null)?.use {
if (it.fileDescriptor != null) {
with(FileOutputStream(it.fileDescriptor)) {
compress(
Bitmap.CompressFormat.PNG,
DEFAULT_IMAGE_QUALITY,
this
)
flush()
close()
}
}
}
} catch …Run Code Online (Sandbox Code Playgroud) 这是我将byte []数组转换为图像的代码
unsafe
{
//convert the UInt32[] into byte array and then assign the pointer to it
fixed (byte* ptr = Misc.ConvertFromUInt32Array(image))
{
Bitmap bmp = new Bitmap(200,64,800,
PixelFormat.Format32bppRgb,
new IntPtr(ptr));
bmp.Save("test.bmp");
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了这个:
alt text http:////img11.imageshack.us/img11/4853/testacr.png
代码中的问题在哪里,为什么会发生这种情况?如何将其恢复正常?