Graphics Question

D

Darren Clark

I am trying to simply embed a opaque image over another image...

if i use a windows form it works..

e.g.

Graphics g = this.pictureBox1.CreateGraphics();


g.Clear(this.BackColor);


Bitmap bitmap = new Bitmap(@"d:\\club_home_page_feature.jpg");

float[][] ptsArray ={

new float[] {1, 0, 0, 0, 0},

new float[] {0, 1, 0, 0, 0},

new float[] {0, 0, 1, 0, 0},

new float[] {0, 0, 0, 0.5f, 0},

new float[] {0, 0, 0, 0, 1}};

Rectangle rect = new Rectangle(0,0,bitmap.Width,bitmap.Height);

//Rectangle rect = new Rectangle20, 20, 200, 100);

ColorMatrix clrMatrix = new ColorMatrix(ptsArray);


ImageAttributes imgAttributes = new ImageAttributes();

imgAttributes.SetColorMatrix(clrMatrix,

ColorMatrixFlag.Default,

ColorAdjustType.Bitmap);

g.FillRectangle(Brushes.Black, rect);

g.DrawImage(bitmap,

new Rectangle(0, 0, bitmap.Width, bitmap.Height),

0, 0, bitmap.Width, bitmap.Height,

GraphicsUnit.Pixel, imgAttributes);

g.Dispose();



however how can i then save this newly createed image to the files sytem?
 
O

Oliver Sturm

Darren said:
however how can i then save this newly createed image to the files sytem?

I guess you should first create an image that holds all the combined
information:

Bitmap bitmap = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bitmap);

// now draw whatever you want into g
// you can also save the bitmap however you want

// Last, get the Graphics for your picture box and copy the
// complete image there

Graphics pictureBoxGraphics = pictureBox1.CreateGraphics();
pictureBoxGraphics.DrawImage(bitmap, 0, 0);


Oliver Sturm
 
S

Ski

to save it as a jpeg do something like this

EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 90;
EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality,
quality);
encoderParams.Param[0] = encoderParam;

ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
break;
}
}

if (jpegICI != null)
{
bm.Save(sOutputFilename, jpegICI, encoderParams);
}

bm is the bitmap you have drawn on.

hth
Scott
 
D

Darren Clark

Whenever it saves to a file it save as a black image?
Any ideas?

Bitmap bitmap = new Bitmap(@"d:\\club_home_page_feature.jpg");

Bitmap nImage = new Bitmap(bitmap,200,200);

Graphics g = Graphics.FromImage(nImage);

float[][] ptsArray ={

new float[] {1, 0, 0, 0, 0},

new float[] {0, 1, 0, 0, 0},

new float[] {0, 0, 1, 0, 0},

new float[] {0, 0, 0, 0.5f, 0},

new float[] {0, 0, 0, 0, 1}};

Rectangle rect = new Rectangle(0,0,bitmap.Width,bitmap.Height);

//Rectangle rect = new Rectangle20, 20, 200, 100);

nImage.Save("d:\\1.jpg");

ColorMatrix clrMatrix = new ColorMatrix(ptsArray);


ImageAttributes imgAttributes = new ImageAttributes();

imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);

g.FillRectangle(Brushes.Black, rect);

g.DrawImage(nImage, new Rectangle(0, 0, bitmap.Width, bitmap.Height),

0, 0, bitmap.Width, bitmap.Height,

GraphicsUnit.Pixel, imgAttributes);

nImage.Save("d:\\2.jpg");

g.Dispose();

nImage.Save("d:\\3.jpg");

Graphics pictureBoxGraphics = this.pictureBox1.CreateGraphics();

pictureBoxGraphics.DrawImage(nImage, 0, 0);
 
O

Oliver Sturm

Darren said:
Whenever it saves to a file it save as a black image?
Any ideas?

I haven't tried your code, but I found that you seem to have a few
things mangled up a bit. Maybe it has to do with that. See below.
Bitmap bitmap = new Bitmap(@"d:\\club_home_page_feature.jpg");

Bitmap nImage = new Bitmap(bitmap,200,200);

Graphics g = Graphics.FromImage(nImage);

float[][] ptsArray ={

new float[] {1, 0, 0, 0, 0},

new float[] {0, 1, 0, 0, 0},

new float[] {0, 0, 1, 0, 0},

new float[] {0, 0, 0, 0.5f, 0},

new float[] {0, 0, 0, 0, 1}};

Rectangle rect = new Rectangle(0,0,bitmap.Width,bitmap.Height);

//Rectangle rect = new Rectangle20, 20, 200, 100);

nImage.Save("d:\\1.jpg");

At a glance, I would expect that this saved image shouldn't be black? Is it?
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);


ImageAttributes imgAttributes = new ImageAttributes();

imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);

g.FillRectangle(Brushes.Black, rect);

Now here you fill the complete nImage with black, don't you?
g.DrawImage(nImage, new Rectangle(0, 0, bitmap.Width, bitmap.Height),

0, 0, bitmap.Width, bitmap.Height,

GraphicsUnit.Pixel, imgAttributes);


Now you draw the image nImage on its own canvas (which is g, remember),
using a rectangle with the height and width of the original loaded
image? I can't imagine the result of that, but I suppose it would still
be black, regardless of the attributes you pass in.
nImage.Save("d:\\2.jpg");

Black, I guess.
g.Dispose();

nImage.Save("d:\\3.jpg");

Black, still.
Graphics pictureBoxGraphics = this.pictureBox1.CreateGraphics();

pictureBoxGraphics.DrawImage(nImage, 0, 0);

Black, sure.


Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top