all groups > vj# > july 2004 >
You're in the

vj#

group:

Image -> .bmp or .gif


Image -> .bmp or .gif miriam_adrian NO[at]SPAM gmx.de
7/29/2004 3:12:16 AM
vj#:
Hello,

actually I try to save an image with j#. I've searched the help, there
I've found this line:
FileStream fs = new FileStream("C:\\winnt\\Gone Fishing2.BMP",
FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);

The problem is: I do not have a ubyte array (MyData). I only have an
Image object an an int array with the RGB information.

Is there any way to write an Image object directly to file or to
convert the Image object to an ubyte array?

I would be very glad if someone answers! Thank You.

Re: Image -> .bmp or .gif Lars-Inge Tønnessen
7/29/2004 3:34:11 PM
Hello Miriam,

Below I have written an example (J# Win Forms Application) for you (in
J#.net Visual Studio 2003). Please see //__FROM HERE__ to //___TO HERE___,
the rest is GUI generated by Visual Studio.


The source image is in an int array with RGB colors like this:
int[][][] image_array = new int[255][255][3];
255 is the x and y size.
3 is the RGB colors (1 for red, 1 for green and 1 for blue)

The destination varable has the same xand y size (both 255 for x and y):
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(255, 255);


This is for generating a nice int-array source image effect.
// Generate a sample [red][green][blue] image
for( int y = 0; y < 255; y++ )
for ( int x = 0; x < 255; x++ )
{
image_array[x][y][0] = y;
image_array[x][y][1] = x;
image_array[x][y][2] = 100;
}


This line will show the image in a picture GUI control in the Win Forms
Application.
this.pictureBox1.set_Image( bitmap );


These lines gets a filename from a SaveAs dialog, and stores the image as
JPG/BMP/GIF formats to disk files if the user pushes "Save/OK".

System.Windows.Forms.DialogResult result =
this.saveFileDialog1.ShowDialog();

if ( result == System.Windows.Forms.DialogResult.OK )
{
bitmap.Save( this.saveFileDialog1.get_FileName()+".jpg",
System.Drawing.Imaging.ImageFormat.get_Jpeg() );
bitmap.Save( this.saveFileDialog1.get_FileName()+".gif",
System.Drawing.Imaging.ImageFormat.get_Gif() );
bitmap.Save( this.saveFileDialog1.get_FileName()+".bmp",
System.Drawing.Imaging.ImageFormat.get_Bmp() );
}


______ THE COMPLETE CODE - "Form1.jsl" file________

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

#region Windows Form Designer generated code
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.set_Location(new System.Drawing.Point(16, 16));
this.button1.set_Name("button1");
this.button1.set_TabIndex(0);
this.button1.set_Text("Save");
this.button1.add_Click( new System.EventHandler(this.button1_Click) );
//
// pictureBox1
//
this.pictureBox1.set_Location(new System.Drawing.Point(8, 48));
this.pictureBox1.set_Name("pictureBox1");
this.pictureBox1.set_Size(new System.Drawing.Size(280, 208));
this.pictureBox1.set_TabIndex(1);
this.pictureBox1.set_TabStop(false);
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(292, 266));
this.get_Controls().Add(this.pictureBox1);
this.get_Controls().Add(this.button1);
this.set_Name("Form1");
this.set_Text("Form1");
this.ResumeLayout(false);

}
#endregion

/** @attribute System.STAThread() */
public static void main(String[] args)
{
Application.Run(new Form1());
}

// ________FROM HERE _________________________
private void button1_Click (Object sender, System.EventArgs e)
{
// Source image
int[][][] image_array = new int[255][255][3];

// Generate a sample [red][green][blue] image
for( int y = 0; y < 255; y++ )
for ( int x = 0; x < 255; x++ )
{
image_array[x][y][0] = y;
image_array[x][y][1] = x;
image_array[x][y][2] = 100;
}

// Destination image
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(255, 255);

// Make the destination image.
for( int y = 0; y < 255; y++ )
for ( int x = 0; x < 255; x++ )
{
System.Drawing.Color color =
System.Drawing.Color.FromArgb(
image_array[x][y][0], // red
image_array[x][y][1], // green
image_array[x][y][2]); // blue
bitmap.SetPixel(x,y, color );
}

// View the picture in the GUI
this.pictureBox1.set_Image( bitmap );

// Get the filename
System.Windows.Forms.DialogResult result =
this.saveFileDialog1.ShowDialog();

// Save the image as JPG, GIF and BMP
if ( result == System.Windows.Forms.DialogResult.OK )

if ( result == System.Windows.Forms.DialogResult.OK )
{
bitmap.Save( this.saveFileDialog1.get_FileName()+".jpg",
System.Drawing.Imaging.ImageFormat.get_Jpeg() );
bitmap.Save( this.saveFileDialog1.get_FileName()+".gif",
System.Drawing.Imaging.ImageFormat.get_Gif() );
bitmap.Save( this.saveFileDialog1.get_FileName()+".bmp",
System.Drawing.Imaging.ImageFormat.get_Bmp() );
}

}
// ________TO HERE _________________________

}


Regards,
Lars-Inge Tønnessen
www.larsinge.com


Re: Image -> .bmp or .gif miriam_adrian NO[at]SPAM gmx.de
7/30/2004 3:22:26 AM
Hello Lars-Inge,

thanks! It runs. I'm very happy :-).

Regards,

Miriam Adrian


[quoted text, click to view]
Re: Image -> .bmp or .gif Lars-Inge Tønnessen
7/30/2004 1:24:35 PM

I forgot to tell you if you want to run this in a console application,
remember to add a reference to the "System.Drawing" dll. =:o)



Regards,
Lars-Inge Tønnessen
www.larsinge.com

AddThis Social Bookmark Button