How to create an image from scratch?

Hi everybody,
I’m actually testing XoJo which is very good product. I 've a basic question.
How to create by my self a simple RGBA image?
I’ve tested unsuccessfully this code
What’s is wrong
Thanks a lot and regards from France

 Dim pic As  new Picture (160,120)
 DIM  mbRGBA as new MemoryBlock (160 * 120 * 4)  

  dim offset as integer
  offset =0
  
  for i = 0 to 119
    for j = 0 to 159
      mbRGBA.Byte(offset) = 255
      mbRGBA.Byte(offset + 1) = 0
      mbRGBA.Byte(offset + 2) = 0
      mbRGBA.Byte(offset + 3) = 0
      offset = offset + 4
    next j
  next i

  pic = Picture.FromData(mbRGBA)
  
  Imagewell1.Image = pic

What exactly are you trying to do?
It looks like you are trying to build an image byte by byte…
Is there a reason you can not DRAW your picture and then save it using the desired format, and let the system figure out the internal encoding required?

Plus I think your “format” is wrong… there is no header information (required even by a simple BMP format)

maybe better use new picture, than get RGBSurface and change pixels?

Thanks for replies
In fact I’m using OpenCV associated to XoJo for processing binaries thermical images (very easy with Declare)
This code is OK

  For i = 0 To n step 2
    lo = sbin.ReadByte()                                               'low byte for grayscale
    mbPixels.byte(index) = lo                                     
    hi =sbin.ReadByte()                                                  'high byte for Temperature calculus
    temperature =((hi * 256+ lo) -1000)  / 10             'temp in degrees
    mbTemperatures.Byte(index) = hi
    TemperatureTotale.Append (temperature)             'store temperature in array
    for j = 0 to 3
      mbRGBA.Byte (j + (index * 4)) = lo                       'create A RGBA image : OK A channel not processed by OpenCV
    next
    index= index + 1
  Next

and this instruction cvSetData(src, mbRGBA, 160 * 4 ) creates a perfect 4-channel image than OpenCV can use
src is just a pointer to an image structure
I’m just wondering why Picture.FromData(mbRGBA) does not work
Regards

Thanks Christian
Your suggestion was right: using RBGSurface is perfect in association with a MemoryBlock used to store binary values :slight_smile:
Can be used whatever the number of channels of the binary image

  dim lastX as integer = pic.Width -1
  dim lastY as integer = pic.Height - 1
  
  
  index = 0
  nChannels = 1
  For y As Integer = 0 To endY
    For x As Integer = 0 To endX
      index= (y * 160) + (x * nChannels) 
      aColor= RGB(mbPixels.Byte(index), mbPixels.Byte(index), mbPixels.Byte(index), 0)
      pic.RGBSurface.Pixel(x,y) = aColor
    next
  next
  
  // Display the picture
  Pic.Graphics.DrawPicture(pic, 0,0)
  Imagewell1.Image = pic