|
-
Mar 7th, 2011, 10:14 AM
#1
Thread Starter
Lively Member
[RESOLVED] SetPixel is not supported for images with indexed pixel formats
Hi guys,
I am loading an image on picturebox, then trying to paint some particular pixel but it does not work for the tif image that I have. I got the following error message "SetPixel is not supported for images with indexed pixel formats."
Any help will be appreciated.
-
Mar 7th, 2011, 02:47 PM
#2
Frenzied Member
Re: SetPixel is not supported for images with indexed pixel formats
If the image you're trying to draw on has an indexed pixel format, then you need to create a bitmap object with a non-indexed pixel format and draw the tif image to it so you can draw on it.
Example:
vb Code:
Dim tifimage as Image = Image.FromFile("C:\MyTif.TIF") 'indexed format image.
Dim NonIndexedImage as new Bitmap(tifimage.width,tifimage.height,System.Drawing.Imaging.PixelFormat.Format32bppPargB)
Dim g as Graphics = Graphics.FromImage(Ctype(NonIndexedImage,Image))
g.DrawImage(tifimage,0,0)
g.dispose()
tifimage.dispose()
'now you can setpixel on the tif image with the NonIndexedImage object.
NonIndexedImage.SetPixel(0,0,Color.Blue)
'any further modifications
...
'then save
NonIndexedImage.save("C:\MyTif.TIF",System.Drawing.Imaging.ImageFormat.Tiff)
Hope this helps,
Justin
Last edited by MonkOFox; Mar 7th, 2011 at 02:52 PM.
-
Mar 7th, 2011, 05:39 PM
#3
Thread Starter
Lively Member
Re: SetPixel is not supported for images with indexed pixel formats
Thank you, Justin for your help. It works now.
I just added a button to the program to clear the modified image from picturebox.
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
PictureBox1.Image.Dispose()
End Sub
but unfortunately the image was not cleared. There is no error message either. I don't want the user to close the program and run it again in order to load another image. How can I do that?
Thanks.
-
Mar 8th, 2011, 09:14 AM
#4
Frenzied Member
Re: SetPixel is not supported for images with indexed pixel formats
Try:
PictureBox1.Image = Nothing
You may have to refresh the picturebox control, but probably not.
P.S.
All .dispose does is release the resources that are handling the Image object (Locks and etc.). You need to do that, but just wanted to let you know.
I think the picturebox loads the file into memory but if you write 'Dim img as Image = Image.FromFile(Path as string)', that would put a lock on the file and to remove it you
would need to call .Dispose on that image object in order to get rid of that lock.
Justin
Last edited by MonkOFox; Mar 8th, 2011 at 09:18 AM.
-
Mar 10th, 2011, 03:04 PM
#5
Thread Starter
Lively Member
Re: SetPixel is not supported for images with indexed pixel formats
Thanks Justin. It works like a charm. I appreciated your help.
-
Mar 10th, 2011, 05:08 PM
#6
Frenzied Member
Re: [RESOLVED] SetPixel is not supported for images with indexed pixel formats
No problem man, glad you got it working : ).
Justin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|