【发布时间】:2014-04-07 00:34:52
【问题描述】:
到目前为止,我已经得到了什么,从中学习
https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx
https://stackoverflow.com/a/4619004/3487239
https://stackoverflow.com/a/22855181/3487239
open System.Drawing
open System.Drawing.Imaging
let cat = new Bitmap("cat.jpg") // original image of cat
let pixels (image:Bitmap) = // BitLocking and Unlocking an Image
let Width = image.Width
let Height = image.Height
let rect = new Rectangle(0,0,Width,Height)
// Lock the image for access
let data = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat)
// Copy the data
let ptr = data.Scan0
let stride = data.Stride
let bytes = stride * data.Height
let values : byte[] = Array.zeroCreate bytes
System.Runtime.InteropServices.Marshal.Copy(ptr,values,0,bytes)
let pixelSize = 4 // <-- calculate this from the PixelFormat
// All the data has been inserted into byte[] values
(*
This block of code is where I go through the byte[] values from above
to do all sorts of image manipulation eg. changing color of pixels etc
Which I have completed, just need to save the image.
*)
// My Question
(*
In here, how to turn the modified byte[] values back to Bitmap before
unlocking the bits?
*)
// Unlock the image
image.UnlockBits(data)
pixels cat // modified version of the same image
到目前为止我已经尝试过,(新的内存流到新的位图,新的位图构造函数,然后是 bitmap.save 等等......)
https://stackoverflow.com/a/22890620/3487239
https://stackoverflow.com/a/3801289/3487239
代码使用上述方法编译,但它会创建同一图像的另一个副本,而不是所需的结果。
【问题讨论】:
-
@downvoters 请自行解释。 OP 是一个 SO 初学者,他试图巩固一系列问题。如果问题是评论应该是问题中的文本,而不是代码中的 cmets,请明确。我投了一个+1,我通常不会参与其中,因为没有解释的投反对票只是IMO的一个坏主意
-
可能是我问了太多问题,或者没有认真思考。两天后我会将此问题标记为已解决,因为我已经找到了答案。
-
There are lots of subtle and not so subtle ways in which your questions can be improved。 NB 最好有 3 或 4 个问题,它们的差异足以将它们分开。在您的具体情况下,可能是时候发布这样的摘要帖子了。对于每个问题(尤其是这样的摘要)来说,从上到下保持可读性是很重要的,必须通过链接到其他问题来了解它的要点。随着时间的推移,你会学到这一切——尤其是当其他人评论和反对时!
-
好的,我看到了很棒的投票,表明你还活着并且在倾听。但是除非问题实际上得到改善,否则我的赞成票将变成反对票。照原样 - 它需要工作,如果您认为自己没有时间将其更改为其他人可以实际阅读和理解的内容,则应将其删除。
-
我认为您应该尽可能避免互操作/编组。您应该能够使用 .NET 类库中内置的 API 来执行此操作。参见例如stackoverflow.com/a/14337202/5652483