Page 1 of 1

How convert pixel format?

Posted: Sun Jan 29, 2012 11:22 pm
by Nandika
Hello friends,
i want to convert pixel format of any image to normal RGB32bit pixel format image using VB.NET 2010.i have alredy done with VB.NET 2005.
i searched using Object browser.no success.
plz tel me a function to do that.

thanks

[ Post made via Mobile Device ] Image

Re: How convert pixel format?

Posted: Mon Jan 30, 2012 4:48 pm
by Saman
Why don't you use simple mathematics to convert from one format to the other?

Re: How convert pixel format?

Posted: Mon Jan 30, 2012 6:32 pm
by Neo
If the following code helps, try to convert it to VB.Net. It's in C#.

Code: Select all

Bitmap img = (Bitmap)Image.FromFile(dlg.FileName);

// Ensure that it's a 32 bit per pixel file
if (img.PixelFormat!=PixelFormat.Format32bppPArgb){

	Bitmap temp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppPArgb);
	Graphics g = Graphics.FromImage(temp);
	g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
	img.Dispose();
	g.Dispose();
	img = temp;
}

Re: How convert pixel format?

Posted: Tue Jan 31, 2012 3:55 pm
by Nandika
Thank Neo,
actually it was helpful.i could convert it very easily.
This is converted code.

Code: Select all

imports system.drawing
imports system.drawing.imagine

public class Form1
private sub Btn_click()
dim img as bitmap=new bitmap(application.startuppath+"/tux.png")

'cheack pixel format of opend image is 32bppRGB
if not img.pixelformat=pixelformat.format32bppRgb then
dim tmp as bitmap(img.width,img.height,pixelformat.format32bpp)

dim g as graphics=graphics.fromimage(tmp)
g.drawimage(img,new rectangle(0,0,img.width,img.height),0,0,img.width,img.height,graphicsunit.pixel

tmp.save(application.startuppath+"/tux.bmp")
end if
end sub
end class
i have used Format32bppRgb insted of Format32bppArgb.because i want it.

thank Neo

[ Post made via Mobile Device ] Image

Re: How convert pixel format?

Posted: Tue Jan 31, 2012 4:14 pm
by Nandika
Hello Saman,
What is simpal mathematical system which you knew.
please tel me about it.
i have very small knowledge about images,pixel,file types,colours,...
i knew it from explore Python image libarary files in ubuntu.

thank

[ Post made via Mobile Device ] Image

Re: How convert pixel format?

Posted: Wed Feb 01, 2012 1:12 pm
by Nandika
Saman why are you silent?
can you help me?please
i like to know your system.it will be important to me and all ECs.
i have alredy searched in EC. i couldnt find anyone.

thank

[ Post made via Mobile Device ] Image

Re: How convert pixel format?

Posted: Wed Feb 01, 2012 7:59 pm
by Saman
Sorry dude, I was busy.

Neo has added a code and glad to know that you got it working. Your post meant like a feature which was removed from the latest .Net. Even in a case like that, you can use simple transformations to convert from one colour plane to the other.

For example, one of the very common transformation is between YUV and RGB. Usually cameras output data in YUV format and you need to get it converted to RGB to display on a PC. Again, if you need to convert to JPG or MPEG, you need to convert it back from RGB to YUV. Here are the transformations.

RGB to YUV Conversion

Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128

YUV to RGB Conversion

B = 1.164(Y - 16) + 2.018(U - 128)
G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
R = 1.164(Y - 16) + 1.596(V - 128)

In your case, RGB32 is about having three 8-bit (3 bytes) for RGB data and one extra byte of Alpha.

Re: How convert pixel format?

Posted: Wed Feb 01, 2012 11:55 pm
by Nandika
Thank you Saman,
it is really nice and very simple system.actually it will be important to me in future.
i have small memory about this. this had in Python image library.

Thank you Saman for dedicate your valuale time fore me.

[ Post made via Mobile Device ] Image