473,545 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Clip a portion of an Image

HI All,

I have an Image. I want to clip a portion of it and copy to another image.
How to do this? I know the bounding rectangle to clip.

Any suggestions please.

Thanks for your time and help.

Regards
Anand Ganesh
Nov 13 '05 #1
15 31776
Create a graphics object from a bitmap and draw into it using the
image from the other graphics object. You can specify the source and
dest rects.

Jonathan Schafer

On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
HI All,

I have an Image. I want to clip a portion of it and copy to another image.
How to do this? I know the bounding rectangle to clip.

Any suggestions please.

Thanks for your time and help.

Regards
Anand Ganesh


Nov 13 '05 #2
I tried this but I got an error
An unhandled exception of type 'System.Excepti on' occurred in
system.drawing. dll

Additional information: A Graphics object cannot be created from an image
that has an indexed pixel format.
But the problem is after clipping I need another Image object itself. The
destination should be an image but the imae should be a clipped image of the
bigger Image.

Any more suggestions please?

Regards
Anand Ganesh
"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:q5******** *************** *********@4ax.c om...
Create a graphics object from a bitmap and draw into it using the
image from the other graphics object. You can specify the source and
dest rects.

Jonathan Schafer

On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
HI All,

I have an Image. I want to clip a portion of it and copy to another image.How to do this? I know the bounding rectangle to clip.

Any suggestions please.

Thanks for your time and help.

Regards
Anand Ganesh

Nov 13 '05 #3
The following code works for me. It loads a .gif, draws the 30 x 30
section of the gif into an offscreen bitmap, and then draws the scaled
image into a picturebox.

System.Drawing. Bitmap bmp = new Bitmap(30, 30);
Image
img=System.Draw ing.Image.FromF ile(@"C:\cygwin \bin\openssl-0.9.6d\doc\open ssl_button.gif" );
Graphics g1 = pictureBox1.Cre ateGraphics();
g1.DrawImage(im g, 0, 0, img.Width, img.Height);
g1.Dispose();
Graphics g3 = Graphics.FromIm age(bmp);
g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height);
Graphics g2 = pictureBox2.Cre ateGraphics();
g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height);
g2.Dispose();
g3.Dispose();
bmp.Dispose();
img.Dispose();
Jonathan Schafer

On Sun, 13 Jul 2003 21:53:20 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
I tried this but I got an error
An unhandled exception of type 'System.Excepti on' occurred in
system.drawing .dll

Additional information: A Graphics object cannot be created from an image
that has an indexed pixel format.
But the problem is after clipping I need another Image object itself. The
destination should be an image but the imae should be a clipped image of the
bigger Image.

Any more suggestions please?

Regards
Anand Ganesh
"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:q5******* *************** **********@4ax. com...
Create a graphics object from a bitmap and draw into it using the
image from the other graphics object. You can specify the source and
dest rects.

Jonathan Schafer

On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
>HI All,
>
>I have an Image. I want to clip a portion of it and copy to anotherimage. >How to do this? I know the bounding rectangle to clip.
>
>Any suggestions please.
>
>Thanks for your time and help.
>
>Regards
>Anand Ganesh
>


Nov 13 '05 #4
Hi,

See you have a .gif image. I have .tif image. May be that is the problem.
Also I am not sure how to get another image itself; instead of drawing to
the user, I should get a portion of the image and save in a file.

Regards
Anand Ganesh

"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:e9******** *************** *********@4ax.c om...
The following code works for me. It loads a .gif, draws the 30 x 30
section of the gif into an offscreen bitmap, and then draws the scaled
image into a picturebox.

System.Drawing. Bitmap bmp = new Bitmap(30, 30);
Image
img=System.Draw ing.Image.FromF ile(@"C:\cygwin \bin\openssl-0.9.6d\doc\open ssl
_button.gif"); Graphics g1 = pictureBox1.Cre ateGraphics();
g1.DrawImage(im g, 0, 0, img.Width, img.Height);
g1.Dispose();
Graphics g3 = Graphics.FromIm age(bmp);
g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height);
Graphics g2 = pictureBox2.Cre ateGraphics();
g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height);
g2.Dispose();
g3.Dispose();
bmp.Dispose();
img.Dispose();
Jonathan Schafer

On Sun, 13 Jul 2003 21:53:20 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
I tried this but I got an error
An unhandled exception of type 'System.Excepti on' occurred in
system.drawing .dll

Additional information: A Graphics object cannot be created from an image
that has an indexed pixel format.
But the problem is after clipping I need another Image object itself. The
destination should be an image but the imae should be a clipped image of thebigger Image.

Any more suggestions please?

Regards
Anand Ganesh
"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:q5******* *************** **********@4ax. com...
Create a graphics object from a bitmap and draw into it using the
image from the other graphics object. You can specify the source and
dest rects.

Jonathan Schafer

On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:

>HI All,
>
>I have an Image. I want to clip a portion of it and copy to another

image.
>How to do this? I know the bounding rectangle to clip.
>
>Any suggestions please.
>
>Thanks for your time and help.
>
>Regards
>Anand Ganesh
>

Nov 13 '05 #5
Shouldn't matter. A gif file is a 256 color palette indexed image. I
only used it to show that using indexed images works.

Maybe you could post a sample of your code to see what you doing.

Jonathan Schafer

On Mon, 14 Jul 2003 09:11:54 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
Hi,

See you have a .gif image. I have .tif image. May be that is the problem.
Also I am not sure how to get another image itself; instead of drawing to
the user, I should get a portion of the image and save in a file.

Regards
Anand Ganesh

"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:e9******* *************** **********@4ax. com...
The following code works for me. It loads a .gif, draws the 30 x 30
section of the gif into an offscreen bitmap, and then draws the scaled
image into a picturebox.

System.Drawing. Bitmap bmp = new Bitmap(30, 30);
Image

img=System.Dra wing.Image.From File(@"C:\cygwi n\bin\openssl-0.9.6d\doc\open ssl
_button.gif" );
Graphics g1 = pictureBox1.Cre ateGraphics();
g1.DrawImage(im g, 0, 0, img.Width, img.Height);
g1.Dispose();
Graphics g3 = Graphics.FromIm age(bmp);
g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height);
Graphics g2 = pictureBox2.Cre ateGraphics();
g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height);
g2.Dispose();
g3.Dispose();
bmp.Dispose();
img.Dispose();
Jonathan Schafer

On Sun, 13 Jul 2003 21:53:20 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
>I tried this but I got an error
>
>
>An unhandled exception of type 'System.Excepti on' occurred in
>system.drawing .dll
>
>Additional information: A Graphics object cannot be created from an image
>that has an indexed pixel format.
>
>
>But the problem is after clipping I need another Image object itself. The
>destination should be an image but the imae should be a clipped image ofthe >bigger Image.
>
>Any more suggestions please?
>
>Regards
>Anand Ganesh
>
>
>"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
>news:q5******* *************** **********@4ax. com...
>> Create a graphics object from a bitmap and draw into it using the
>> image from the other graphics object. You can specify the source and
>> dest rects.
>>
>> Jonathan Schafer
>>
>> On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
>> <an**********@h otmail.com> wrote:
>>
>> >HI All,
>> >
>> >I have an Image. I want to clip a portion of it and copy to another
>image.
>> >How to do this? I know the bounding rectangle to clip.
>> >
>> >Any suggestions please.
>> >
>> >Thanks for your time and help.
>> >
>> >Regards
>> >Anand Ganesh
>> >
>>
>


Nov 13 '05 #6
I've given you the sample code that does just that. It loads a gif
file, copies a 30 x 30 portion ( rect(0, 0, 29, 29) of the original
into the new bitmap, and draws both to a picturebox to ensure that I
am cropping the image correctly.

What more do you want?

Jonathan Schafer
On Mon, 14 Jul 2003 10:42:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
I don't have a code yet. This is what I wanted to do. Create a small clipped
image from a bigger image and found there is no easy way and I need some
help.

Any suggestions please?

"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:ac******* *************** **********@4ax. com...
Shouldn't matter. A gif file is a 256 color palette indexed image. I
only used it to show that using indexed images works.

Maybe you could post a sample of your code to see what you doing.

Jonathan Schafer

On Mon, 14 Jul 2003 09:11:54 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
>Hi,
>
>See you have a .gif image. I have .tif image. May be that is the problem.
>Also I am not sure how to get another image itself; instead of drawing to
>the user, I should get a portion of the image and save in a file.
>
>Regards
>Anand Ganesh
>
>"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
>news:e9******* *************** **********@4ax. com...
>> The following code works for me. It loads a .gif, draws the 30 x 30
>> section of the gif into an offscreen bitmap, and then draws the scaled
>> image into a picturebox.
>>
>> System.Drawing. Bitmap bmp = new Bitmap(30, 30);
>> Image
>>


img=System.Dr awing.Image.Fro mFile(@"C:\cygw in\bin\openssl-0.9.6d\doc\open ss

l
>_button.gif" );
>> Graphics g1 = pictureBox1.Cre ateGraphics();
>> g1.DrawImage(im g, 0, 0, img.Width, img.Height);
>> g1.Dispose();
>> Graphics g3 = Graphics.FromIm age(bmp);
>> g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height);
>> Graphics g2 = pictureBox2.Cre ateGraphics();
>> g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height);
>> g2.Dispose();
>> g3.Dispose();
>> bmp.Dispose();
>> img.Dispose();
>>
>>
>> Jonathan Schafer
>>
>> On Sun, 13 Jul 2003 21:53:20 -0700, "Anand Ganesh"
>> <an**********@h otmail.com> wrote:
>>
>> >I tried this but I got an error
>> >
>> >
>> >An unhandled exception of type 'System.Excepti on' occurred in
>> >system.drawing .dll
>> >
>> >Additional information: A Graphics object cannot be created from animage >> >that has an indexed pixel format.
>> >
>> >
>> >But the problem is after clipping I need another Image object itself.The >> >destination should be an image but the imae should be a clipped imageof >the
>> >bigger Image.
>> >
>> >Any more suggestions please?
>> >
>> >Regards
>> >Anand Ganesh
>> >
>> >
>> >"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote inmessage >> >news:q5******* *************** **********@4ax. com...
>> >> Create a graphics object from a bitmap and draw into it using the
>> >> image from the other graphics object. You can specify the sourceand >> >> dest rects.
>> >>
>> >> Jonathan Schafer
>> >>
>> >> On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
>> >> <an**********@h otmail.com> wrote:
>> >>
>> >> >HI All,
>> >> >
>> >> >I have an Image. I want to clip a portion of it and copy to another
>> >image.
>> >> >How to do this? I know the bounding rectangle to clip.
>> >> >
>> >> >Any suggestions please.
>> >> >
>> >> >Thanks for your time and help.
>> >> >
>> >> >Regards
>> >> >Anand Ganesh
>> >> >
>> >>
>> >
>>
>


Nov 13 '05 #7
For you to write it, of course!! Now aren't you silly?

Sorry to be cranky... I've been arguing with people all day about
self-motivation and the idea of "helping those who help themselves".

Jonathan Schafer <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message news:<ni******* *************** **********@4ax. com>...
I've given you the sample code that does just that. It loads a gif
file, copies a 30 x 30 portion ( rect(0, 0, 29, 29) of the original
into the new bitmap, and draws both to a picturebox to ensure that I
am cropping the image correctly.

What more do you want?

Jonathan Schafer

Nov 13 '05 #8
Hi ,

I am sorry if I am troubling you or may be I am having difficulty
understanding the code.
Here is your code and my understanding

System.Drawing. Bitmap bmp = new Bitmap(30, 30); [ Creates an
empty bmp of size 30 x 30 ]
Image
img=System.Draw ing.Image.FromF ile(@"C:\cygwin \bin\openssl-0.9.6d\doc\open ssl
_button.gif"); [ Creates an Image Object ]
Graphics g1 = pictureBox1.Cre ateGraphics();
[ Creates a Graphics Object ]
g1.DrawImage(im g, 0, 0, img.Width, img.Height);
[ Draws the Img in the pictureBox1 using the Graphics g1 with full Img width
and height]
g1.Dispose();
[Disposed g1]
Graphics g3 = Graphics.FromIm age(bmp);
[Creating another Graphics from the Empty Bitmap 30x30]
g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height); [Drawing the
unscaled image of Img also extracting the specified 30x30 portion from the
img ] [Is the clipping occurring here?]
Graphics g2 = pictureBox2.Cre ateGraphics();
[creating g2]
g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height); [Drawing the bmp
in the picturebox]
g2.Dispose();
[From here below all are disposed]
g3.Dispose();
bmp.Dispose();
img.Dispose();

Here is my code. All I am doing is trying to clip a portion and copy to
clipboard, it is exactly same as yours. But it doesn't work.

System.Drawing. Bitmap TheClippedBmp = new Bitmap((int)thi s.ImageRect.Wid th,
(int)this.Image Rect.Height);

Graphics Gra = Graphics.FromIm age(TheClippedB mp);

Gra.DrawImageUn scaled(this.The ImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(in t) ImageRect.Width ,(int) ImageRect.Heigh t);

System.Windows. Forms.Clipboard .SetDataObject( TheClippedBmp) ;

Gra.Dispose() ;

TheClippedBmp.D ispose() ;

this.statusBarP anelIBMsg.Text = "Data copied successfully !" ;

Can you tell me am I missing something here?

I really wish this will be a great lesson for me which I will not forget in
my lifetime if you could say what I am missing.

Thanks for spending your precious time to teach Newbie like me.

Regards

Anand Ganesh


"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:ni******** *************** *********@4ax.c om...
I've given you the sample code that does just that. It loads a gif
file, copies a 30 x 30 portion ( rect(0, 0, 29, 29) of the original
into the new bitmap, and draws both to a picturebox to ensure that I
am cropping the image correctly.

What more do you want?

Jonathan Schafer
On Mon, 14 Jul 2003 10:42:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
I don't have a code yet. This is what I wanted to do. Create a small clippedimage from a bigger image and found there is no easy way and I need some
help.

Any suggestions please?

"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:ac******* *************** **********@4ax. com...
Shouldn't matter. A gif file is a 256 color palette indexed image. I
only used it to show that using indexed images works.

Maybe you could post a sample of your code to see what you doing.

Jonathan Schafer

On Mon, 14 Jul 2003 09:11:54 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:

>Hi,
>
>See you have a .gif image. I have .tif image. May be that is the problem. >Also I am not sure how to get another image itself; instead of drawing to >the user, I should get a portion of the image and save in a file.
>
>Regards
>Anand Ganesh
>
>"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message >news:e9******* *************** **********@4ax. com...
>> The following code works for me. It loads a .gif, draws the 30 x 30
>> section of the gif into an offscreen bitmap, and then draws the scaled >> image into a picturebox.
>>
>> System.Drawing. Bitmap bmp = new Bitmap(30, 30);
>> Image
>>
img=System.Dr awing.Image.Fro mFile(@"C:\cygw in\bin\openssl-0.9.6d\doc\open s

sl
>_button.gif" );
>> Graphics g1 = pictureBox1.Cre ateGraphics();
>> g1.DrawImage(im g, 0, 0, img.Width, img.Height);
>> g1.Dispose();
>> Graphics g3 = Graphics.FromIm age(bmp);
>> g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height);
>> Graphics g2 = pictureBox2.Cre ateGraphics();
>> g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height);
>> g2.Dispose();
>> g3.Dispose();
>> bmp.Dispose();
>> img.Dispose();
>>
>>
>> Jonathan Schafer
>>
>> On Sun, 13 Jul 2003 21:53:20 -0700, "Anand Ganesh"
>> <an**********@h otmail.com> wrote:
>>
>> >I tried this but I got an error
>> >
>> >
>> >An unhandled exception of type 'System.Excepti on' occurred in
>> >system.drawing .dll
>> >
>> >Additional information: A Graphics object cannot be created from an

image
>> >that has an indexed pixel format.
>> >
>> >
>> >But the problem is after clipping I need another Image object itself.
The
>> >destination should be an image but the imae should be a clipped
imageof
>the
>> >bigger Image.
>> >
>> >Any more suggestions please?
>> >
>> >Regards
>> >Anand Ganesh
>> >
>> >
>> >"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in

message
>> >news:q5******* *************** **********@4ax. com...
>> >> Create a graphics object from a bitmap and draw into it using the
>> >> image from the other graphics object. You can specify the source

and
>> >> dest rects.
>> >>
>> >> Jonathan Schafer
>> >>
>> >> On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
>> >> <an**********@h otmail.com> wrote:
>> >>
>> >> >HI All,
>> >> >
>> >> >I have an Image. I want to clip a portion of it and copy to

another >> >image.
>> >> >How to do this? I know the bounding rectangle to clip.
>> >> >
>> >> >Any suggestions please.
>> >> >
>> >> >Thanks for your time and help.
>> >> >
>> >> >Regards
>> >> >Anand Ganesh
>> >> >
>> >>
>> >
>>
>

Nov 13 '05 #9
It is not throwing Exception but I am expecting it will copy the clipped
image into a Clipboard, which it is not doing. I am really surprised.

This is such a simple issue to copy a portion of the image to clipboard and
I am not able to do.

I am really glad I have a friend who is helping me out.

This is an Image Control I am writing. This has a toolbar and few buttons.
All the buttons are working fine except the code for this "Copy" button.

Once the user zooms in to the portion of the image and when he clicks copy,
I want that zoomed portion of the image to be transfered to the clipboard.

Here is the code after the user clicks the button.

The ImageRect is a RectangleF object and I am manipulating its values as and
when the user zooms in or zooms out or pan. Then applying this ImageRect on
the original image so that the right portion of the image is drawn for the
user.

TheImage to draw is an Image Object, which has the original image.

The test image I am using has a dimension of 3592 x 2485. It is a .tif image
file.

private void toolBarIB_Butto nClick(object sender,
System.Windows. Forms.ToolBarBu ttonClickEventA rgs e)

{

this.Current_To olName = e.Button.Text ;

if (e.Button.Text == "FZoomIn")

{

float WidthFactor = ImageRect.Width / 5 ;

float HeightFactor = ImageRect.Heigh t / 5 ;

ImageRect.X = ImageRect.X + WidthFactor ;

ImageRect.Y = ImageRect.Y + HeightFactor ;

ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ;

ImageRect.Heigh t = ImageRect.Heigh t - (2 * HeightFactor) ;

this.Cursor = System.Windows. Forms.Cursors.D efault ;

Invalidate() ;

}

else if (e.Button.Text == "FZoomOut")

{

float WidthFactor = ImageRect.Width / 5 ;

float HeightFactor = ImageRect.Heigh t / 5 ;

ImageRect.X = ImageRect.X - WidthFactor ;

ImageRect.Y = ImageRect.Y - HeightFactor ;

ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ;

ImageRect.Heigh t = ImageRect.Heigh t + (2 * HeightFactor) ;

this.Cursor = System.Windows. Forms.Cursors.D efault ;

Invalidate() ;

}

else if (e.Button.Text == "Pan")

{

this.Cursor = new Cursor(this.Get Type(), "HandCursor.cur ");

}

else if (e.Button.Text == "FullExtent ")

{

this.ImageRect = this.Initial_Im ageRect ;

Invalidate() ;

}

else if (e.Button.Text == "ZoomIn")

{

this.Cursor = System.Windows. Forms.Cursors.C ross ;

}

else if (e.Button.Text == "ZoomOut")

{

this.Cursor = System.Windows. Forms.Cursors.C ross ;

}

else if (e.Button.Text == "Copy")

{

try

{

System.Drawing. Bitmap TheClippedBmp = new Bitmap((int)thi s.ImageRect.Wid th,
(int)this.Image Rect.Height);

Graphics Gra = Graphics.FromIm age(TheClippedB mp);

Gra.DrawImageUn scaled(this.The ImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(in t) ImageRect.Width ,(int) ImageRect.Heigh t);

System.Windows. Forms.Clipboard .SetDataObject( TheClippedBmp) ;

Gra.Dispose() ;

TheClippedBmp.D ispose() ;

this.statusBarP anelIBMsg.Text = "Data copied successfully !" ;

}

catch(Exception ex)

{

MessageBox.Show (ex.Message) ;

}

}

else if (e.Button.Text == "Measure")

{

this.Cursor = System.Windows. Forms.Cursors.C ross ;

}

}
"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:cj******** *************** *********@4ax.c om...
See embedded comments below:

Jonathan Schafer

On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:
Hi ,

I am sorry if I am troubling you or may be I am having difficulty
understandin g the code.
Here is your code and my understanding

System.Drawing .Bitmap bmp = new Bitmap(30, 30); [ Creates an
empty bmp of size 30 x 30 ]
Image


img=System.Dra wing.Image.From File(@"C:\cygwi n\bin\openssl-0.9.6d\doc\open ss

l
_button.gif" ); [ Creates an Image Object ]
Graphics g1 = pictureBox1.Cre ateGraphics();
[ Creates a Graphics Object ]
g1.DrawImage(i mg, 0, 0, img.Width, img.Height);
[ Draws the Img in the pictureBox1 using the Graphics g1 with full Img width
and height]
g1.Dispose() ;
[Disposed g1]
Graphics g3 = Graphics.FromIm age(bmp);
[Creating another Graphics from the Empty Bitmap 30x30]
g3.DrawImageUn scaled(img, 0, 0, bmp.Width, bmp.Height); [Drawing the
unscaled image of Img also extracting the specified 30x30 portion from theimg ] [Is the clipping occurring here?]
Graphics g2 = pictureBox2.Cre ateGraphics();
[creating g2]
g2.DrawImageUn scaled(bmp, 0, 0, bmp.Width, bmp.Height); [Drawing the bmpin the picturebox]
g2.Dispose() ;
[From here below all are disposed]
g3.Dispose() ;
bmp.Dispose( );
img.Dispose( );


Your comments regarding the code above are correct. The clipping
occurs on g3.DrawImageUns caled. What I've done is created a 30 x 30
rectangle of the original bitmap and copied it into a second bitmap,
thus clipping the rest of the bitmap off.
Here is my code. All I am doing is trying to clip a portion and copy to
clipboard, it is exactly same as yours. But it doesn't work.

System.Drawing .Bitmap TheClippedBmp = new Bitmap((int)thi s.ImageRect.Wid th,(int)this.Imag eRect.Height);

Graphics Gra = Graphics.FromIm age(TheClippedB mp);

Gra.DrawImageU nscaled(this.Th eImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(i nt) ImageRect.Width ,(int) ImageRect.Heigh t);

System.Windows .Forms.Clipboar d.SetDataObject (TheClippedBmp) ;

Gra.Dispose( ) ;

TheClippedBmp. Dispose() ;

this.statusBar PanelIBMsg.Text = "Data copied successfully !" ;

Can you tell me am I missing something here?


So far as I can tell, this looks correct. What part doesn't work?
Are you getting an exception? If so, what is the exception and on
which line does it occur?

If you aren't getting an exception but the result is incorrect, what
part is incorrect.

If your image is small, perhaps you code attach a .zip file with the
image and your .cs file containing only the code necessary to crop the
bitmap and I could look at it in a project here.


I really wish this will be a great lesson for me which I will not forget inmy lifetime if you could say what I am missing.

Thanks for spending your precious time to teach Newbie like me.

Regards

Anand Ganesh


"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message
news:ni******* *************** **********@4ax. com...
I've given you the sample code that does just that. It loads a gif
file, copies a 30 x 30 portion ( rect(0, 0, 29, 29) of the original
into the new bitmap, and draws both to a picturebox to ensure that I
am cropping the image correctly.

What more do you want?

Jonathan Schafer
On Mon, 14 Jul 2003 10:42:11 -0700, "Anand Ganesh"
<an**********@h otmail.com> wrote:

>I don't have a code yet. This is what I wanted to do. Create a small

clipped
>image from a bigger image and found there is no easy way and I need some >help.
>
>Any suggestions please?
>
>"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in message >news:ac******* *************** **********@4ax. com...
>> Shouldn't matter. A gif file is a 256 color palette indexed image. I >> only used it to show that using indexed images works.
>>
>> Maybe you could post a sample of your code to see what you doing.
>>
>> Jonathan Schafer
>>
>>
>>
>> On Mon, 14 Jul 2003 09:11:54 -0700, "Anand Ganesh"
>> <an**********@h otmail.com> wrote:
>>
>> >Hi,
>> >
>> >See you have a .gif image. I have .tif image. May be that is the

problem.
>> >Also I am not sure how to get another image itself; instead of drawing
to
>> >the user, I should get a portion of the image and save in a file.
>> >
>> >Regards
>> >Anand Ganesh
>> >
>> >"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in

message
>> >news:e9******* *************** **********@4ax. com...
>> >> The following code works for me. It loads a .gif, draws the 30 x
30 >> >> section of the gif into an offscreen bitmap, and then draws the

scaled
>> >> image into a picturebox.
>> >>
>> >> System.Drawing. Bitmap bmp = new Bitmap(30, 30);
>> >> Image
>> >>
>>

img=System.D rawing.Image.Fr omFile(@"C:\cyg win\bin\openssl-0.9.6d\doc\open ss
>l
>> >_button.gif" );
>> >> Graphics g1 = pictureBox1.Cre ateGraphics();
>> >> g1.DrawImage(im g, 0, 0, img.Width, img.Height);
>> >> g1.Dispose();
>> >> Graphics g3 = Graphics.FromIm age(bmp);
>> >> g3.DrawImageUns caled(img, 0, 0, bmp.Width, bmp.Height);
>> >> Graphics g2 = pictureBox2.Cre ateGraphics();
>> >> g2.DrawImageUns caled(bmp, 0, 0, bmp.Width, bmp.Height);
>> >> g2.Dispose();
>> >> g3.Dispose();
>> >> bmp.Dispose();
>> >> img.Dispose();
>> >>
>> >>
>> >> Jonathan Schafer
>> >>
>> >> On Sun, 13 Jul 2003 21:53:20 -0700, "Anand Ganesh"
>> >> <an**********@h otmail.com> wrote:
>> >>
>> >> >I tried this but I got an error
>> >> >
>> >> >
>> >> >An unhandled exception of type 'System.Excepti on' occurred in
>> >> >system.drawing .dll
>> >> >
>> >> >Additional information: A Graphics object cannot be created from

an >image
>> >> >that has an indexed pixel format.
>> >> >
>> >> >
>> >> >But the problem is after clipping I need another Image object

itself.
>The
>> >> >destination should be an image but the imae should be a clipped

image
>of
>> >the
>> >> >bigger Image.
>> >> >
>> >> >Any more suggestions please?
>> >> >
>> >> >Regards
>> >> >Anand Ganesh
>> >> >
>> >> >
>> >> >"Jonathan Schafer" <jschafer@*NOSP AM*brierley.a.b .c.com> wrote in >message
>> >> >news:q5******* *************** **********@4ax. com...
>> >> >> Create a graphics object from a bitmap and draw into it using the >> >> >> image from the other graphics object. You can specify the source >and
>> >> >> dest rects.
>> >> >>
>> >> >> Jonathan Schafer
>> >> >>
>> >> >> On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh"
>> >> >> <an**********@h otmail.com> wrote:
>> >> >>
>> >> >> >HI All,
>> >> >> >
>> >> >> >I have an Image. I want to clip a portion of it and copy to

another
>> >> >image.
>> >> >> >How to do this? I know the bounding rectangle to clip.
>> >> >> >
>> >> >> >Any suggestions please.
>> >> >> >
>> >> >> >Thanks for your time and help.
>> >> >> >
>> >> >> >Regards
>> >> >> >Anand Ganesh
>> >> >> >
>> >> >>
>> >> >
>> >>
>> >
>>
>

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4679
by: Jeff Thies | last post by:
I'm having trouble clipping an image. My understanding is that this is all I should need: <img src="test_img.jpg" style="clip: rect(10px 10px 10px 10px)" alt="alt"> Although I've seen this as a comma seperated list. This has no effect whatesoever. What am I doing wrong? Jeff
2
5764
by: Kimmo R. M. Hovi | last post by:
Currently I have defined: -- clip clip -- <form method="post" action="xx" enctype="application/x-www-form-urlencoded"> <input type="image" name="Function 1" src="image1.gif" border="0" title="1">
0
1291
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and help. Regards
0
1331
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and help. Regards
6
1834
by: queisser | last post by:
Hi all, I'm writing a C# app that uses copy/paste to put graphics in a window. My copy/paste logic first looks for Metafile, then for bitmap. Here's the problem: when I copy and paste a WMF clip from the organizer (in Word or standalone) into my app I get a nice high-res image the first time I copy a particular clip. The second time I get...
8
3924
by: Warren Post | last post by:
At <http://snow.prohosting.com/srcopan/dry/test.es.html>, you will see that the background image runs down the left margin. The right hand side of it is faded, watermark style, but it is one single image. my intention is for the header and text to appear to the right of the unfaded portion of the image, and on top of the watermarked portion....
2
3717
by: elephant | last post by:
I'm trying something in CSS, and I was wondering if anyone knows of a way to do it. The goal is to have one image with a width of 25px, and have the left 12 pixels be one side of an image of indeterminate width, the right 12 pixels be the right side of the image, and the center pixel to stretch the entire distance in between. I know how to...
5
5947
cameokid
by: cameokid | last post by:
Can someone help me out with this. Here is what i am trying to do. I have an image of size 310x310. Initially i am displaying only 50x50 using clip property. Later using setTimeout property i am trying to get the entire image so that it looks like animation. But this doesn't happen with the code below <html> <head> <style type="text/css">...
29
4054
by: Chris Riesbeck | last post by:
I have an image with a class and the class defines a clip rectangle. In Firefox 2 and 3, and Opera 9, I can access the rectangle with document.defaultView.getComputedStyle(). But that doesn't seem to work in Safari for Windows 3, nor when I use image.currentStyle.clip in IE 7. Is there a way to do this in those browsers? Am I doing...
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.