How convert pixel format?

.NET programming topics
Post Reply
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

How convert pixel format?

Post by Nandika » Sun Jan 29, 2012 11:22 pm

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
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How convert pixel format?

Post by Saman » Mon Jan 30, 2012 4:48 pm

Why don't you use simple mathematics to convert from one format to the other?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How convert pixel format?

Post by Neo » Mon Jan 30, 2012 6:32 pm

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;
}
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How convert pixel format?

Post by Nandika » Tue Jan 31, 2012 3:55 pm

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
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How convert pixel format?

Post by Nandika » Tue Jan 31, 2012 4:14 pm

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
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How convert pixel format?

Post by Nandika » Wed Feb 01, 2012 1:12 pm

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
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How convert pixel format?

Post by Saman » Wed Feb 01, 2012 7:59 pm

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.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How convert pixel format?

Post by Nandika » Wed Feb 01, 2012 11:55 pm

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
Post Reply

Return to “.NET Programming”