2012-07-31 4 views
0

이미지 너비/높이/보폭과 버퍼가 있습니다.stride/buffer/width/height를 비트 맵으로 변환하십시오.

어떻게이 정보를 System.Drawing.Bitmap으로 변환합니까? 이 4 가지가 있으면 원본 이미지를 다시 가져올 수 있습니까?

+0

HTTP 작동합니다 : //whathaveyoutried.com/... 보여 일부를 코드 ... –

+0

당신은 이진 형식으로 이미지를 데이터베이스에 저장하고 값을 검색하고 이미지로 표시하려고 할 때 업로드하는 것을 말합니까? –

+0

내 말은 누군가가 비디오 카메라의 높이/너비/보폭/버퍼를 얻기위한 코드를 작성했다는 것입니다. 그는 지금 내가 그것에 물건을 할 싶어. 비트 맵 이미지를 사용하고 writeablebitmap을 사용하여 스트라이드/너비 등을 얻는 방법을 알려주었습니다. – Mattb2291

답변

1

당신이 가지고있는 모든 것을 (플러스 PixelFormat)을 필요로하는 Bitmap 생성자 오버로드가있다 :

public Bitmap(int width, int height, int stride, PixelFormat format, IntPtr scan0);

(args.Buffer 예를 들어 byte 같은 blittable 유형의 배열 인 경우)이 작동하지 않을 수 있습니다 :

Bitmap bitmap; 
var gch = System.Runtime.InteropServices.GCHandle.Alloc(args.Buffer, GCHandleType.Pinned); 
try 
{ 
    bitmap = new Bitmap(
     args.Width, args.Height, args.Stride, 
     System.Drawing.Imaging.PixelFormat.Format24bppRgb, 
     gch.AddrOfPinnedObject()); 
} 
finally 
{ 
    gch.Free(); 
} 

업데이트 :

Probabl 그것은 새로 생성 된 이미지 바이트를 수동으로 복사하는 것이 더 낫습니다. 왜냐하면 그 생성자가 그렇게하지 않는 것처럼 보이기 때문이며, byte[] 배열의 이미지 데이터가 가비지 수집되면 모든 종류의 나쁜 일이 발생할 수 있습니다.

var bitmap = new Bitmap(args.Width, args.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
var data = bitmap.LockBits(
    new Rectangle(0, 0, args.Width, args.Height), 
    System.Drawing.Imaging.ImageLockMode.WriteOnly, 
    System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

if(data.Stride == args.Stride) 
{ 
    Marshal.Copy(args.Buffer, 0, data.Scan0, args.Stride * args.Height); 
} 
else 
{ 
    int arrayOffset = 0; 
    int imageOffset = 0; 
    for(int y = 0; y < args.Height; ++y) 
    { 
     Marshal.Copy(args.Buffer, arrayOffset, (IntPtr)(((long)data.Scan0) + imageOffset), data.Stride); 
     arrayOffset += args.Stride; 
     imageOffset += data.Stride; 
    } 
} 

bitmap.UnlockBits(data); 
+0

내가받은 것은 args에서 스트라이드/버퍼 등을 찾을 수 있다는 것입니다. ie -> args .Buffer = buffer ... args.Stride = stride etc 그러나 사용 비트 맵 테스트 = 새 비트 맵 (args.Width, args.Height, args.Stride, PixelFormats.Rgb24, args.Buffer); 이 나에게 오류를 제공 :(나는 ...? – Mattb2291

+0

이 작품은 ... 음 ... 어딘가에서 나를 얻을 것인가 ... 이미지 프로그래밍에 좀 새로운 해요 var에 테스트 = BitmapFrame.Create (args.Width, args.Height, 300D, 300D, PixelFormats.Rgb24, null, args.Buffer, args.Stride); – Mattb2291

+0

어떤 타입이'args.Buffer'?'byte []'입니까? 또한'System.Drawing.Bitmap'과'BitmapFrame '두 개의 다른 GUI 프레임 워크에서 왔으므로, 무엇을 사용해야할지 결정해야합니다. – max

0

당신은 바이트 [], 폭과 높이 +를 pixelformat (보폭)와 같은 버퍼가있는 경우이

public Bitmap CreateBitmapFromRawDataBuffer(int width, int height, PixelFormat imagePixelFormat, byte[] buffer) 
    { 
     Size imageSize = new Size(width, height); 

     Bitmap bitmap = new Bitmap(imageSize.Width, imageSize.Height, imagePixelFormat); 
     Rectangle wholeBitmap = new Rectangle(0, 0, bitmap.Width, bitmap.Height); 

     // Lock all bitmap's pixels. 
     BitmapData bitmapData = bitmap.LockBits(wholeBitmap, ImageLockMode.WriteOnly, imagePixelFormat); 

     // Copy the buffer into bitmapData. 
     System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bitmapData.Scan0, buffer.Length); 

     // Unlock all bitmap's pixels. 
     bitmap.UnlockBits(bitmapData); 

     return bitmap; 
    } 
관련 문제