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 15 31529
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**********@hotmail.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
I tried this but I got an error
An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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
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.Drawing.Image.FromFile(@"C:\cygwin\bin\ openssl-0.9.6d\doc\openssl_button.gif");
Graphics g1 = pictureBox1.CreateGraphics();
g1.DrawImage(img, 0, 0, img.Width, img.Height);
g1.Dispose();
Graphics g3 = Graphics.FromImage(bmp);
g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height);
Graphics g2 = pictureBox2.CreateGraphics();
g2.DrawImageUnscaled(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**********@hotmail.com> wrote: I tried this but I got an error
An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >
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@*NOSPAM*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.Drawing.Image.FromFile(@"C:\cygwin\bin\ openssl-0.9.6d\doc\openssl
_button.gif"); Graphics g1 = pictureBox1.CreateGraphics(); g1.DrawImage(img, 0, 0, img.Width, img.Height); g1.Dispose(); Graphics g3 = Graphics.FromImage(bmp); g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); Graphics g2 = pictureBox2.CreateGraphics(); g2.DrawImageUnscaled(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**********@hotmail.com> wrote:
I tried this but I got an error
An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >
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**********@hotmail.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@*NOSPAM*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.Drawing.Image.FromFile(@"C:\cygwin\bin \openssl-0.9.6d\doc\openssl _button.gif"); Graphics g1 = pictureBox1.CreateGraphics(); g1.DrawImage(img, 0, 0, img.Width, img.Height); g1.Dispose(); Graphics g3 = Graphics.FromImage(bmp); g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); Graphics g2 = pictureBox2.CreateGraphics(); g2.DrawImageUnscaled(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**********@hotmail.com> wrote:
>I tried this but I got an error > > >An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >> > >> >
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**********@hotmail.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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.Drawing.Image.FromFile(@"C:\cygwin\bi n\openssl-0.9.6d\doc\openss l >_button.gif"); >> Graphics g1 = pictureBox1.CreateGraphics(); >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> g1.Dispose(); >> Graphics g3 = Graphics.FromImage(bmp); >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> Graphics g2 = pictureBox2.CreateGraphics(); >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >I tried this but I got an error >> > >> > >> >An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >> >> > >> >> >> > >> >
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@*NOSPAM*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
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.Drawing.Image.FromFile(@"C:\cygwin\bin\ openssl-0.9.6d\doc\openssl
_button.gif"); [ Creates an Image Object ]
Graphics g1 = pictureBox1.CreateGraphics();
[ Creates a Graphics Object ]
g1.DrawImage(img, 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.FromImage(bmp);
[Creating another Graphics from the Empty Bitmap 30x30]
g3.DrawImageUnscaled(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.CreateGraphics();
[creating g2]
g2.DrawImageUnscaled(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)this.ImageRect.Width,
(int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheCl ippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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.Drawing.Image.FromFile(@"C:\cygwin\bi n\openssl-0.9.6d\doc\opens
sl >_button.gif"); >> Graphics g1 = pictureBox1.CreateGraphics(); >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> g1.Dispose(); >> Graphics g3 = Graphics.FromImage(bmp); >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> Graphics g2 = pictureBox2.CreateGraphics(); >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >I tried this but I got an error >> > >> > >> >An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >> >> > >> >> >> > >> >
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_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
this.Current_ToolName = e.Button.Text ;
if (e.Button.Text == "FZoomIn")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X + WidthFactor ;
ImageRect.Y = ImageRect.Y + HeightFactor ;
ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "FZoomOut")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X - WidthFactor ;
ImageRect.Y = ImageRect.Y - HeightFactor ;
ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "Pan")
{
this.Cursor = new Cursor(this.GetType(), "HandCursor.cur");
}
else if (e.Button.Text == "FullExtent")
{
this.ImageRect = this.Initial_ImageRect ;
Invalidate() ;
}
else if (e.Button.Text == "ZoomIn")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "ZoomOut")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "Copy")
{
try
{
System.Drawing.Bitmap TheClippedBmp = new Bitmap((int)this.ImageRect.Width,
(int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheCl ippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.Text = "Data copied successfully !" ;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message) ;
}
}
else if (e.Button.Text == "Measure")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
}
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:cj********************************@4ax.com... See embedded comments below:
Jonathan Schafer
On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
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.Drawing.Image.FromFile(@"C:\cygwin\bin \openssl-0.9.6d\doc\openss
l _button.gif"); [ Creates an Image Object ] Graphics g1 = pictureBox1.CreateGraphics(); [ Creates a Graphics Object ] g1.DrawImage(img, 0, 0, img.Width, img.Height); [ Draws the Img in the pictureBox1 using the Graphics g1 with full Img
widthand height] g1.Dispose(); [Disposed g1] Graphics g3 = Graphics.FromImage(bmp); [Creating another Graphics from the Empty Bitmap 30x30] g3.DrawImageUnscaled(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.CreateGraphics(); [creating g2] g2.DrawImageUnscaled(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.DrawImageUnscaled. 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)this.ImageRect.Width,(int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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**********@hotmail.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
drawingto >> >the user, I should get a portion of the image and save in a file. >> > >> >Regards >> >Anand Ganesh >> > >> >"Jonathan Schafer" <jschafer@*NOSPAM*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.Drawing.Image.FromFile(@"C:\cygwin\b in\openssl-0.9.6d\doc\open
ss >l >> >_button.gif"); >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> g1.Dispose(); >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >I tried this but I got an error >> >> > >> >> > >> >> >An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >> >> >> > >> >> >> >> >> > >> >> >> > >> >
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_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
this.Current_ToolName = e.Button.Text ;
if (e.Button.Text == "FZoomIn")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X + WidthFactor ;
ImageRect.Y = ImageRect.Y + HeightFactor ;
ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "FZoomOut")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X - WidthFactor ;
ImageRect.Y = ImageRect.Y - HeightFactor ;
ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "Pan")
{
this.Cursor = new Cursor(this.GetType(), "HandCursor.cur");
}
else if (e.Button.Text == "FullExtent")
{
this.ImageRect = this.Initial_ImageRect ;
Invalidate() ;
}
else if (e.Button.Text == "ZoomIn")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "ZoomOut")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "Copy")
{
try
{
System.Drawing.Bitmap TheClippedBmp = new Bitmap((int)this.ImageRect.Width,
(int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheCl ippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.Text = "Data copied successfully !" ;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message) ;
}
}
else if (e.Button.Text == "Measure")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
}
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:cj********************************@4ax.com... See embedded comments below:
Jonathan Schafer
On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
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.Drawing.Image.FromFile(@"C:\cygwin\bin \openssl-0.9.6d\doc\openss
l _button.gif"); [ Creates an Image Object ] Graphics g1 = pictureBox1.CreateGraphics(); [ Creates a Graphics Object ] g1.DrawImage(img, 0, 0, img.Width, img.Height); [ Draws the Img in the pictureBox1 using the Graphics g1 with full Img
widthand height] g1.Dispose(); [Disposed g1] Graphics g3 = Graphics.FromImage(bmp); [Creating another Graphics from the Empty Bitmap 30x30] g3.DrawImageUnscaled(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.CreateGraphics(); [creating g2] g2.DrawImageUnscaled(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.DrawImageUnscaled. 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)this.ImageRect.Width,(int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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**********@hotmail.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
drawingto >> >the user, I should get a portion of the image and save in a file. >> > >> >Regards >> >Anand Ganesh >> > >> >"Jonathan Schafer" <jschafer@*NOSPAM*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.Drawing.Image.FromFile(@"C:\cygwin\b in\openssl-0.9.6d\doc\open
ss >l >> >_button.gif"); >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> g1.Dispose(); >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >I tried this but I got an error >> >> > >> >> > >> >> >An unhandled exception of type 'System.Exception' 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@*NOSPAM*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**********@hotmail.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 >> >> >> > >> >> >> >> >> > >> >> >> > >> >
I really don't see anything wrong with the code. A couple of things
you can do...
1. Can you draw the cropped image onto a window or picturebox to see
if the image is in the cropped bitmap and is correct.
2. Is your application still running when you are looking at the
contents of the clipboard? The reason I ask is that you are not
specifying to retain the data in the clipboard when your application
exits (the overloaded method takes two parameters, the second a bool
indicating whether to keep the data in the clipboard when the app
exits.
Jonathan Schafer
On Tue, 15 Jul 2003 09:23:03 -0700, "Anand Ganesh"
<an**********@hotmail.com> wrote: 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_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArg s e)
{
this.Current_ToolName = e.Button.Text ;
if (e.Button.Text == "FZoomIn")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X + WidthFactor ;
ImageRect.Y = ImageRect.Y + HeightFactor ;
ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "FZoomOut")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X - WidthFactor ;
ImageRect.Y = ImageRect.Y - HeightFactor ;
ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "Pan")
{
this.Cursor = new Cursor(this.GetType(), "HandCursor.cur");
}
else if (e.Button.Text == "FullExtent")
{
this.ImageRect = this.Initial_ImageRect ;
Invalidate() ;
}
else if (e.Button.Text == "ZoomIn")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "ZoomOut")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "Copy")
{
try
{
System.Drawing.Bitmap TheClippedBmp = new Bitmap((int)this.ImageRect.Width, (int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.Text = "Data copied successfully !" ;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message) ;
}
}
else if (e.Button.Text == "Measure")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
}
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message news:cj********************************@4ax.com.. . See embedded comments below:
Jonathan Schafer
On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
>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.Drawing.Image.FromFile(@"C:\cygwin\bi n\openssl-0.9.6d\doc\openss l >_button.gif"); [ Creates an Image Object ] >Graphics g1 = pictureBox1.CreateGraphics(); >[ Creates a Graphics Object ] >g1.DrawImage(img, 0, 0, img.Width, img.Height); >[ Draws the Img in the pictureBox1 using the Graphics g1 with full Imgwidth >and height] >g1.Dispose(); >[Disposed g1] >Graphics g3 = Graphics.FromImage(bmp); >[Creating another Graphics from the Empty Bitmap 30x30] >g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); [Drawing the >unscaled image of Img also extracting the specified 30x30 portion fromthe >img ] [Is the clipping occurring here?] >Graphics g2 = pictureBox2.CreateGraphics(); >[creating g2] >g2.DrawImageUnscaled(bmp, 0, 0, bmp.Width, bmp.Height); [Drawing thebmp >in 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.DrawImageUnscaled. 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 = newBitmap((int)this.ImageRect.Width, >(int)this.ImageRect.Height); > >Graphics Gra = Graphics.FromImage(TheClippedBmp); > >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); > >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; > >Gra.Dispose() ; > >TheClippedBmp.Dispose() ; > >this.statusBarPanelIBMsg.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 forgetin >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@*NOSPAM*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**********@hotmail.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 needsome >> >help. >> > >> >Any suggestions please? >> > >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote inmessage >> >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**********@hotmail.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 ofdrawing >to >> >> >the user, I should get a portion of the image and save in a file. >> >> > >> >> >Regards >> >> >Anand Ganesh >> >> > >> >> >"Jonathan Schafer" <jschafer@*NOSPAM*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 x30 >> >> >> 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.Drawing.Image.FromFile(@"C:\cygwin\ bin\openssl-0.9.6d\doc\opens >s >> >l >> >> >_button.gif"); >> >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> >> g1.Dispose(); >> >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >> >> >I tried this but I got an error >> >> >> > >> >> >> > >> >> >> >An unhandled exception of type 'System.Exception' occurred in >> >> >> >system.drawing.dll >> >> >> > >> >> >> >Additional information: A Graphics object cannot be created froman >> >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@*NOSPAM*brierley.a.b.c.com> wrotein >> >message >> >> >> >news:q5********************************@4ax.com.. . >> >> >> >> Create a graphics object from a bitmap and draw into it usingthe >> >> >> >> image from the other graphics object. You can specify thesource >> >and >> >> >> >> dest rects. >> >> >> >> >> >> >> >> Jonathan Schafer >> >> >> >> >> >> >> >> On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh" >> >> >> >> <an**********@hotmail.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 >> >> >> >> > >> >> >> >> >> >> >> > >> >> >> >> >> > >> >> >> > >> >
1. I tried to draw the clipped bmp in a picture box. Nothing shows up. Which
means it is empty.
[
Here is where I strongly belive in the First place the Image is not clipped.
The reason is as follows
System.Drawing.Bitmap TheClippedBmp = new Bitmap((int)this.ImageRect.Width,
(int)this.ImageRect.Height) ; [This is an Empty Image]
Graphics Gra = Graphics.FromImage(TheClippedBmp) ;
[Graphics Object derived from empty image]
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int)
ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height) ; [A portion of
the original image is drawn on the Graphics object. So no clipping has taken
place.]
Clipping is not happening anywhere in the above steps.
]
2. Yes my application is still running, I am not exiting it after placing
the clipped image in the clipboard.
So I hope now you can understand my problem.
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:85********************************@4ax.com... I really don't see anything wrong with the code. A couple of things you can do...
1. Can you draw the cropped image onto a window or picturebox to see if the image is in the cropped bitmap and is correct.
2. Is your application still running when you are looking at the contents of the clipboard? The reason I ask is that you are not specifying to retain the data in the clipboard when your application exits (the overloaded method takes two parameters, the second a bool indicating whether to keep the data in the clipboard when the app exits.
Jonathan Schafer
On Tue, 15 Jul 2003 09:23:03 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
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
andI 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
andwhen the user zooms in or zooms out or pan. Then applying this ImageRect
onthe original image so that the right portion of the image is drawn for
theuser.
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
imagefile.
private void toolBarIB_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArg s e)
{
this.Current_ToolName = e.Button.Text ;
if (e.Button.Text == "FZoomIn")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X + WidthFactor ;
ImageRect.Y = ImageRect.Y + HeightFactor ;
ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "FZoomOut")
{
float WidthFactor = ImageRect.Width / 5 ;
float HeightFactor = ImageRect.Height / 5 ;
ImageRect.X = ImageRect.X - WidthFactor ;
ImageRect.Y = ImageRect.Y - HeightFactor ;
ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ;
ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ;
this.Cursor = System.Windows.Forms.Cursors.Default ;
Invalidate() ;
}
else if (e.Button.Text == "Pan")
{
this.Cursor = new Cursor(this.GetType(), "HandCursor.cur");
}
else if (e.Button.Text == "FullExtent")
{
this.ImageRect = this.Initial_ImageRect ;
Invalidate() ;
}
else if (e.Button.Text == "ZoomIn")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "ZoomOut")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
else if (e.Button.Text == "Copy")
{
try
{
System.Drawing.Bitmap TheClippedBmp = new
Bitmap((int)this.ImageRect.Width,(int)this.ImageRect.Height);
Graphics Gra = Graphics.FromImage(TheClippedBmp);
Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height);
System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ;
Gra.Dispose() ;
TheClippedBmp.Dispose() ;
this.statusBarPanelIBMsg.Text = "Data copied successfully !" ;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message) ;
}
}
else if (e.Button.Text == "Measure")
{
this.Cursor = System.Windows.Forms.Cursors.Cross ;
}
}
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message news:cj********************************@4ax.com.. . See embedded comments below:
Jonathan Schafer
On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
>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.Drawing.Image.FromFile(@"C:\cygwin\bi n\openssl-0.9.6d\doc\opens
sl >_button.gif"); [ Creates an Image Object ] >Graphics g1 = pictureBox1.CreateGraphics(); >[ Creates a Graphics Object ] >g1.DrawImage(img, 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.FromImage(bmp); >[Creating another Graphics from the Empty Bitmap 30x30] >g3.DrawImageUnscaled(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.CreateGraphics(); >[creating g2] >g2.DrawImageUnscaled(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(); >
Your comments regarding the code above are correct. The clipping occurs on g3.DrawImageUnscaled. 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)this.ImageRect.Width, >(int)this.ImageRect.Height); > >Graphics Gra = Graphics.FromImage(TheClippedBmp); > >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); > >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; > >Gra.Dispose() ; > >TheClippedBmp.Dispose() ; > >this.statusBarPanelIBMsg.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
forgetin >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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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 x30 >> >> >> 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.Drawing.Image.FromFile(@"C:\cygwin\ bin\openssl-0.9.6d\doc\ope
ns >s >> >l >> >> >_button.gif"); >> >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> >> g1.Dispose(); >> >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >> >> >I tried this but I got an error >> >> >> > >> >> >> > >> >> >> >An unhandled exception of type 'System.Exception' occurred in >> >> >> >system.drawing.dll >> >> >> > >> >> >> >Additional information: A Graphics object cannot be created
froman >> >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@*NOSPAM*brierley.a.b.c.com>
wrotein >> >message >> >> >> >news:q5********************************@4ax.com.. . >> >> >> >> Create a graphics object from a bitmap and draw into it
usingthe >> >> >> >> 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**********@hotmail.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 >> >> >> >> > >> >> >> >> >> >> >> > >> >> >> >> >> > >> >> >> > >> >
I was thinking about the clipping and the DrawImageUnscaled won't work
for you. The reason is while it will clip the image, it will always
draw from the 0,0 coordinate of the source image.
Here is the method you need...
Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect,
GraphicsUnit srcUnit);
This allows you to specify the coordinates of the portion of the image
you want to draw from.
So, the code would in fact look like this (more or less)...
Rectangle rect = ImageRect.Truncate();
System.Drawing.Bitmap TheClippedBmp = new Bitmap(rect.Width,
rect.Height) ;
Graphics Gra = Graphics.FromImage(TheClippedBmp) ;
Gra.DrawImage(this.TheImagetoDraw, new Rectangle(0, 0, rect.Width,
rect.Height), rect, GraphicsUnit.Pixel) ;
This should give you the clipped image because you specified with the
source rectangle the area of the original bitmap you want to copy.
Try this and let me know if it works.
Jonathan Schafer
On Tue, 15 Jul 2003 15:07:32 -0700, "Anand Ganesh"
<an**********@hotmail.com> wrote: 1. I tried to draw the clipped bmp in a picture box. Nothing shows up. Which means it is empty.
[
Here is where I strongly belive in the First place the Image is not clipped. The reason is as follows
System.Drawing.Bitmap TheClippedBmp = new Bitmap((int)this.ImageRect.Width, (int)this.ImageRect.Height) ; [This is an Empty Image] Graphics Gra = Graphics.FromImage(TheClippedBmp) ; [Graphics Object derived from empty image] Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height) ; [A portion of the original image is drawn on the Graphics object. So no clipping has taken place.]
Clipping is not happening anywhere in the above steps.
]
2. Yes my application is still running, I am not exiting it after placing the clipped image in the clipboard.
So I hope now you can understand my problem. "Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message news:85********************************@4ax.com.. . I really don't see anything wrong with the code. A couple of things you can do...
1. Can you draw the cropped image onto a window or picturebox to see if the image is in the cropped bitmap and is correct.
2. Is your application still running when you are looking at the contents of the clipboard? The reason I ask is that you are not specifying to retain the data in the clipboard when your application exits (the overloaded method takes two parameters, the second a bool indicating whether to keep the data in the clipboard when the app exits.
Jonathan Schafer
On Tue, 15 Jul 2003 09:23:03 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
>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 clipboardand >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 fewbuttons. >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 clickscopy, >I want that zoomed portion of the image to be transfered to theclipboard. > >Here is the code after the user clicks the button. > >The ImageRect is a RectangleF object and I am manipulating its values asand >when the user zooms in or zooms out or pan. Then applying this ImageRecton >the original image so that the right portion of the image is drawn forthe >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 .tifimage >file. > >private void toolBarIB_ButtonClick(object sender, >System.Windows.Forms.ToolBarButtonClickEventArg s e) > >{ > >this.Current_ToolName = e.Button.Text ; > >if (e.Button.Text == "FZoomIn") > >{ > >float WidthFactor = ImageRect.Width / 5 ; > >float HeightFactor = ImageRect.Height / 5 ; > >ImageRect.X = ImageRect.X + WidthFactor ; > >ImageRect.Y = ImageRect.Y + HeightFactor ; > >ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ; > >ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ; > >this.Cursor = System.Windows.Forms.Cursors.Default ; > >Invalidate() ; > >} > >else if (e.Button.Text == "FZoomOut") > >{ > >float WidthFactor = ImageRect.Width / 5 ; > >float HeightFactor = ImageRect.Height / 5 ; > >ImageRect.X = ImageRect.X - WidthFactor ; > >ImageRect.Y = ImageRect.Y - HeightFactor ; > >ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ; > >ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ; > >this.Cursor = System.Windows.Forms.Cursors.Default ; > >Invalidate() ; > >} > >else if (e.Button.Text == "Pan") > >{ > >this.Cursor = new Cursor(this.GetType(), "HandCursor.cur"); > >} > >else if (e.Button.Text == "FullExtent") > >{ > >this.ImageRect = this.Initial_ImageRect ; > >Invalidate() ; > >} > >else if (e.Button.Text == "ZoomIn") > >{ > >this.Cursor = System.Windows.Forms.Cursors.Cross ; > >} > >else if (e.Button.Text == "ZoomOut") > >{ > >this.Cursor = System.Windows.Forms.Cursors.Cross ; > >} > >else if (e.Button.Text == "Copy") > >{ > >try > >{ > >System.Drawing.Bitmap TheClippedBmp = newBitmap((int)this.ImageRect.Width, >(int)this.ImageRect.Height); > >Graphics Gra = Graphics.FromImage(TheClippedBmp); > >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); > >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; > >Gra.Dispose() ; > >TheClippedBmp.Dispose() ; > >this.statusBarPanelIBMsg.Text = "Data copied successfully !" ; > >} > >catch(Exception ex) > >{ > >MessageBox.Show(ex.Message) ; > >} > >} > >else if (e.Button.Text == "Measure") > >{ > >this.Cursor = System.Windows.Forms.Cursors.Cross ; > >} > >} > >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message >news:cj********************************@4ax.com.. . >> See embedded comments below: >> >> Jonathan Schafer >> >> On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" >> <an**********@hotmail.com> wrote: >> >> >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); [ Createsan >> >empty bmp of size 30 x 30 ] >> >Image >>
img=System.Drawing.Image.FromFile(@"C:\cygwin\b in\openssl-0.9.6d\doc\openss >l >> >_button.gif"); [ Creates an Image Object ] >> >Graphics g1 = pictureBox1.CreateGraphics(); >> >[ Creates a Graphics Object ] >> >g1.DrawImage(img, 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.FromImage(bmp); >> >[Creating another Graphics from the Empty Bitmap 30x30] >> >g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); [Drawingthe >> >unscaled image of Img also extracting the specified 30x30 portion from >the >> >img ] [Is the clipping occurring here?] >> >Graphics g2 = pictureBox2.CreateGraphics(); >> >[creating g2] >> >g2.DrawImageUnscaled(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(); >> > >> >> Your comments regarding the code above are correct. The clipping >> occurs on g3.DrawImageUnscaled. 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 copyto >> >clipboard, it is exactly same as yours. But it doesn't work. >> > >> >System.Drawing.Bitmap TheClippedBmp = new >Bitmap((int)this.ImageRect.Width, >> >(int)this.ImageRect.Height); >> > >> >Graphics Gra = Graphics.FromImage(TheClippedBmp); >> > >> >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >> >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); >> > >> >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; >> > >> >Gra.Dispose() ; >> > >> >TheClippedBmp.Dispose() ; >> > >> >this.statusBarPanelIBMsg.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 notforget >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@*NOSPAM*brierley.a.b.c.com> wrote inmessage >> >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**********@hotmail.com> wrote: >> >> >> >> >I don't have a code yet. This is what I wanted to do. Create asmall >> >clipped >> >> >image from a bigger image and found there is no easy way and I need >some >> >> >help. >> >> > >> >> >Any suggestions please? >> >> > >> >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in >message >> >> >news:ac********************************@4ax.com.. . >> >> >> Shouldn't matter. A gif file is a 256 color palette indexedimage. >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**********@hotmail.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 afile. >> >> >> > >> >> >> >Regards >> >> >> >Anand Ganesh >> >> >> > >> >> >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrotein >> >message >> >> >> >news:e9********************************@4ax.com.. . >> >> >> >> The following code works for me. It loads a .gif, draws the30 x >30 >> >> >> >> section of the gif into an offscreen bitmap, and then drawsthe >> >scaled >> >> >> >> image into a picturebox. >> >> >> >> >> >> >> >> System.Drawing.Bitmap bmp = new Bitmap(30, 30); >> >> >> >> Image >> >> >> >> >> >> >> >> >> >>
>img=System.Drawing.Image.FromFile(@"C:\cygwin \bin\openssl-0.9.6d\doc\open >s >> >s >> >> >l >> >> >> >_button.gif"); >> >> >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> >> >> g1.Dispose(); >> >> >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >I tried this but I got an error >> >> >> >> > >> >> >> >> > >> >> >> >> >An unhandled exception of type 'System.Exception' occurred in >> >> >> >> >system.drawing.dll >> >> >> >> > >> >> >> >> >Additional information: A Graphics object cannot be createdfrom >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 aclipped >> >image >> >> >of >> >> >> >the >> >> >> >> >bigger Image. >> >> >> >> > >> >> >> >> >Any more suggestions please? >> >> >> >> > >> >> >> >> >Regards >> >> >> >> >Anand Ganesh >> >> >> >> > >> >> >> >> > >> >> >> >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com>wrote >in >> >> >message >> >> >> >> >news:q5********************************@4ax.com.. . >> >> >> >> >> Create a graphics object from a bitmap and draw into itusing >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**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >> >> >HI All, >> >> >> >> >> > >> >> >> >> >> >I have an Image. I want to clip a portion of it and copyto >> >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 >> >> >> >> >> > >> >> >> >> >> >> >> >> >> > >> >> >> >> >> >> >> > >> >> >> >> >> > >> >> >> > >> >
Jonathan,
You are a Genius ! Yes it did work.
I am so excited.
Now I learned that applying Gra.DrawImage will draw over the Empty bitmap
file from which the Gra was derived.
I thank you so much for staying so long in the conversation wthout giving
up.
With kind regards
Anand Ganesh
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:e3********************************@4ax.com... I was thinking about the clipping and the DrawImageUnscaled won't work for you. The reason is while it will clip the image, it will always draw from the 0,0 coordinate of the source image.
Here is the method you need...
Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit);
This allows you to specify the coordinates of the portion of the image you want to draw from.
So, the code would in fact look like this (more or less)...
Rectangle rect = ImageRect.Truncate();
System.Drawing.Bitmap TheClippedBmp = new Bitmap(rect.Width, rect.Height) ; Graphics Gra = Graphics.FromImage(TheClippedBmp) ; Gra.DrawImage(this.TheImagetoDraw, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel) ;
This should give you the clipped image because you specified with the source rectangle the area of the original bitmap you want to copy.
Try this and let me know if it works.
Jonathan Schafer
On Tue, 15 Jul 2003 15:07:32 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
1. I tried to draw the clipped bmp in a picture box. Nothing shows up.
Whichmeans it is empty.
[
Here is where I strongly belive in the First place the Image is not
clipped.The reason is as follows
System.Drawing.Bitmap TheClippedBmp = new
Bitmap((int)this.ImageRect.Width,(int)this.ImageRect.Height) ; [This is an Empty Image] Graphics Gra = Graphics.FromImage(TheClippedBmp) ; [Graphics Object derived from empty image] Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height) ; [A portion of the original image is drawn on the Graphics object. So no clipping has
takenplace.]
Clipping is not happening anywhere in the above steps.
]
2. Yes my application is still running, I am not exiting it after placing the clipped image in the clipboard.
So I hope now you can understand my problem. "Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message news:85********************************@4ax.com.. . I really don't see anything wrong with the code. A couple of things you can do...
1. Can you draw the cropped image onto a window or picturebox to see if the image is in the cropped bitmap and is correct.
2. Is your application still running when you are looking at the contents of the clipboard? The reason I ask is that you are not specifying to retain the data in the clipboard when your application exits (the overloaded method takes two parameters, the second a bool indicating whether to keep the data in the clipboard when the app exits.
Jonathan Schafer
On Tue, 15 Jul 2003 09:23:03 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
>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
clipboardand >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
asand >when the user zooms in or zooms out or pan. Then applying this
ImageRecton >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_ButtonClick(object sender, >System.Windows.Forms.ToolBarButtonClickEventArg s e) > >{ > >this.Current_ToolName = e.Button.Text ; > >if (e.Button.Text == "FZoomIn") > >{ > >float WidthFactor = ImageRect.Width / 5 ; > >float HeightFactor = ImageRect.Height / 5 ; > >ImageRect.X = ImageRect.X + WidthFactor ; > >ImageRect.Y = ImageRect.Y + HeightFactor ; > >ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ; > >ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ; > >this.Cursor = System.Windows.Forms.Cursors.Default ; > >Invalidate() ; > >} > >else if (e.Button.Text == "FZoomOut") > >{ > >float WidthFactor = ImageRect.Width / 5 ; > >float HeightFactor = ImageRect.Height / 5 ; > >ImageRect.X = ImageRect.X - WidthFactor ; > >ImageRect.Y = ImageRect.Y - HeightFactor ; > >ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ; > >ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ; > >this.Cursor = System.Windows.Forms.Cursors.Default ; > >Invalidate() ; > >} > >else if (e.Button.Text == "Pan") > >{ > >this.Cursor = new Cursor(this.GetType(), "HandCursor.cur"); > >} > >else if (e.Button.Text == "FullExtent") > >{ > >this.ImageRect = this.Initial_ImageRect ; > >Invalidate() ; > >} > >else if (e.Button.Text == "ZoomIn") > >{ > >this.Cursor = System.Windows.Forms.Cursors.Cross ; > >} > >else if (e.Button.Text == "ZoomOut") > >{ > >this.Cursor = System.Windows.Forms.Cursors.Cross ; > >} > >else if (e.Button.Text == "Copy") > >{ > >try > >{ > >System.Drawing.Bitmap TheClippedBmp = new Bitmap((int)this.ImageRect.Width, >(int)this.ImageRect.Height); > >Graphics Gra = Graphics.FromImage(TheClippedBmp); > >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); > >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; > >Gra.Dispose() ; > >TheClippedBmp.Dispose() ; > >this.statusBarPanelIBMsg.Text = "Data copied successfully !" ; > >} > >catch(Exception ex) > >{ > >MessageBox.Show(ex.Message) ; > >} > >} > >else if (e.Button.Text == "Measure") > >{ > >this.Cursor = System.Windows.Forms.Cursors.Cross ; > >} > >} > >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in
message >news:cj********************************@4ax.com.. . >> See embedded comments below: >> >> Jonathan Schafer >> >> On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" >> <an**********@hotmail.com> wrote: >> >> >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); [
Createsan >> >empty bmp of size 30 x 30 ] >> >Image >> img=System.Drawing.Image.FromFile(@"C:\cygwin\b in\openssl-0.9.6d\doc\open
ss >l >> >_button.gif"); [ Creates an Image Object ] >> >Graphics g1 = pictureBox1.CreateGraphics(); >> >[ Creates a Graphics Object ] >> >g1.DrawImage(img, 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.FromImage(bmp); >> >[Creating another Graphics from the Empty Bitmap 30x30] >> >g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height);
[Drawingthe >> >unscaled image of Img also extracting the specified 30x30 portion
from >the >> >img ] [Is the clipping occurring here?] >> >Graphics g2 = pictureBox2.CreateGraphics(); >> >[creating g2] >> >g2.DrawImageUnscaled(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(); >> > >> >> Your comments regarding the code above are correct. The clipping >> occurs on g3.DrawImageUnscaled. 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
copyto >> >clipboard, it is exactly same as yours. But it doesn't work. >> > >> >System.Drawing.Bitmap TheClippedBmp = new >Bitmap((int)this.ImageRect.Width, >> >(int)this.ImageRect.Height); >> > >> >Graphics Gra = Graphics.FromImage(TheClippedBmp); >> > >> >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >> >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); >> > >> >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; >> > >> >Gra.Dispose() ; >> > >> >TheClippedBmp.Dispose() ; >> > >> >this.statusBarPanelIBMsg.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 >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@*NOSPAM*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**********@hotmail.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@*NOSPAM*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**********@hotmail.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@*NOSPAM*brierley.a.b.c.com>
wrotein >> >message >> >> >> >news:e9********************************@4ax.com.. . >> >> >> >> The following code works for me. It loads a .gif, draws
the30 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.Drawing.Image.FromFile(@"C:\cygwin \bin\openssl-0.9.6d\doc\op
en >s >> >s >> >> >l >> >> >> >_button.gif"); >> >> >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> >> >> g1.Dispose(); >> >> >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >I tried this but I got an error >> >> >> >> > >> >> >> >> > >> >> >> >> >An unhandled exception of type 'System.Exception' occurred
in >> >> >> >> >system.drawing.dll >> >> >> >> > >> >> >> >> >Additional information: A Graphics object cannot be
createdfrom >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@*NOSPAM*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**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >> >> >HI All, >> >> >> >> >> > >> >> >> >> >> >I have an Image. I want to clip a portion of it and
copyto >> >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 >> >> >> >> >> > >> >> >> >> >> >> >> >> >> > >> >> >> >> >> >> >> > >> >> >> >> >> > >> >> >> > >> >
If I were a genius, I would have figured out what was wrong yesterday
and you would have already been past this :). Glad you got it
working.
Regards,
Jonathan Schafer
On Tue, 15 Jul 2003 18:54:31 -0700, "Anand Ganesh"
<an**********@hotmail.com> wrote: Jonathan,
You are a Genius ! Yes it did work.
I am so excited.
Now I learned that applying Gra.DrawImage will draw over the Empty bitmap file from which the Gra was derived.
I thank you so much for staying so long in the conversation wthout giving up.
With kind regards Anand Ganesh
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message news:e3********************************@4ax.com.. . I was thinking about the clipping and the DrawImageUnscaled won't work for you. The reason is while it will clip the image, it will always draw from the 0,0 coordinate of the source image.
Here is the method you need...
Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit);
This allows you to specify the coordinates of the portion of the image you want to draw from.
So, the code would in fact look like this (more or less)...
Rectangle rect = ImageRect.Truncate();
System.Drawing.Bitmap TheClippedBmp = new Bitmap(rect.Width, rect.Height) ; Graphics Gra = Graphics.FromImage(TheClippedBmp) ; Gra.DrawImage(this.TheImagetoDraw, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel) ;
This should give you the clipped image because you specified with the source rectangle the area of the original bitmap you want to copy.
Try this and let me know if it works.
Jonathan Schafer
On Tue, 15 Jul 2003 15:07:32 -0700, "Anand Ganesh" <an**********@hotmail.com> wrote:
>1. I tried to draw the clipped bmp in a picture box. Nothing shows up.Which >means it is empty. > >[ > >Here is where I strongly belive in the First place the Image is notclipped. >The reason is as follows > >System.Drawing.Bitmap TheClippedBmp = newBitmap((int)this.ImageRect.Width, >(int)this.ImageRect.Height) ; [This is an Empty Image] >Graphics Gra = Graphics.FromImage(TheClippedBmp) ; >[Graphics Object derived from empty image] >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height) ; [A portion of >the original image is drawn on the Graphics object. So no clipping hastaken >place.] > >Clipping is not happening anywhere in the above steps. > >] > >2. Yes my application is still running, I am not exiting it after placing >the clipped image in the clipboard. > >So I hope now you can understand my problem. > > > >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message >news:85********************************@4ax.com.. . >> I really don't see anything wrong with the code. A couple of things >> you can do... >> >> 1. Can you draw the cropped image onto a window or picturebox to see >> if the image is in the cropped bitmap and is correct. >> >> 2. Is your application still running when you are looking at the >> contents of the clipboard? The reason I ask is that you are not >> specifying to retain the data in the clipboard when your application >> exits (the overloaded method takes two parameters, the second a bool >> indicating whether to keep the data in the clipboard when the app >> exits. >> >> Jonathan Schafer >> >> On Tue, 15 Jul 2003 09:23:03 -0700, "Anand Ganesh" >> <an**********@hotmail.com> wrote: >> >> >It is not throwing Exception but I am expecting it will copy theclipped >> >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 toclipboard >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 valuesas >and >> >when the user zooms in or zooms out or pan. Then applying thisImageRect >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_ButtonClick(object sender, >> >System.Windows.Forms.ToolBarButtonClickEventArg s e) >> > >> >{ >> > >> >this.Current_ToolName = e.Button.Text ; >> > >> >if (e.Button.Text == "FZoomIn") >> > >> >{ >> > >> >float WidthFactor = ImageRect.Width / 5 ; >> > >> >float HeightFactor = ImageRect.Height / 5 ; >> > >> >ImageRect.X = ImageRect.X + WidthFactor ; >> > >> >ImageRect.Y = ImageRect.Y + HeightFactor ; >> > >> >ImageRect.Width = ImageRect.Width - (2 * WidthFactor) ; >> > >> >ImageRect.Height = ImageRect.Height - (2 * HeightFactor) ; >> > >> >this.Cursor = System.Windows.Forms.Cursors.Default ; >> > >> >Invalidate() ; >> > >> >} >> > >> >else if (e.Button.Text == "FZoomOut") >> > >> >{ >> > >> >float WidthFactor = ImageRect.Width / 5 ; >> > >> >float HeightFactor = ImageRect.Height / 5 ; >> > >> >ImageRect.X = ImageRect.X - WidthFactor ; >> > >> >ImageRect.Y = ImageRect.Y - HeightFactor ; >> > >> >ImageRect.Width = ImageRect.Width + (2 * WidthFactor) ; >> > >> >ImageRect.Height = ImageRect.Height + (2 * HeightFactor) ; >> > >> >this.Cursor = System.Windows.Forms.Cursors.Default ; >> > >> >Invalidate() ; >> > >> >} >> > >> >else if (e.Button.Text == "Pan") >> > >> >{ >> > >> >this.Cursor = new Cursor(this.GetType(), "HandCursor.cur"); >> > >> >} >> > >> >else if (e.Button.Text == "FullExtent") >> > >> >{ >> > >> >this.ImageRect = this.Initial_ImageRect ; >> > >> >Invalidate() ; >> > >> >} >> > >> >else if (e.Button.Text == "ZoomIn") >> > >> >{ >> > >> >this.Cursor = System.Windows.Forms.Cursors.Cross ; >> > >> >} >> > >> >else if (e.Button.Text == "ZoomOut") >> > >> >{ >> > >> >this.Cursor = System.Windows.Forms.Cursors.Cross ; >> > >> >} >> > >> >else if (e.Button.Text == "Copy") >> > >> >{ >> > >> >try >> > >> >{ >> > >> >System.Drawing.Bitmap TheClippedBmp = new >Bitmap((int)this.ImageRect.Width, >> >(int)this.ImageRect.Height); >> > >> >Graphics Gra = Graphics.FromImage(TheClippedBmp); >> > >> >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >> >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); >> > >> >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; >> > >> >Gra.Dispose() ; >> > >> >TheClippedBmp.Dispose() ; >> > >> >this.statusBarPanelIBMsg.Text = "Data copied successfully !" ; >> > >> >} >> > >> >catch(Exception ex) >> > >> >{ >> > >> >MessageBox.Show(ex.Message) ; >> > >> >} >> > >> >} >> > >> >else if (e.Button.Text == "Measure") >> > >> >{ >> > >> >this.Cursor = System.Windows.Forms.Cursors.Cross ; >> > >> >} >> > >> >} >> > >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote inmessage >> >news:cj********************************@4ax.com.. . >> >> See embedded comments below: >> >> >> >> Jonathan Schafer >> >> >> >> On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh" >> >> <an**********@hotmail.com> wrote: >> >> >> >> >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.Drawing.Image.FromFile(@"C:\cygwin\ bin\openssl-0.9.6d\doc\opens >s >> >l >> >> >_button.gif"); [ Creates an Image Object ] >> >> >Graphics g1 = pictureBox1.CreateGraphics(); >> >> >[ Creates a Graphics Object ] >> >> >g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> >[ Draws the Img in the pictureBox1 using the Graphics g1 with fullImg >> >width >> >> >and height] >> >> >g1.Dispose(); >> >> >[Disposed g1] >> >> >Graphics g3 = Graphics.FromImage(bmp); >> >> >[Creating another Graphics from the Empty Bitmap 30x30] >> >> >g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height);[Drawing >the >> >> >unscaled image of Img also extracting the specified 30x30 portionfrom >> >the >> >> >img ] [Is the clipping occurring here?] >> >> >Graphics g2 = pictureBox2.CreateGraphics(); >> >> >[creating g2] >> >> >g2.DrawImageUnscaled(bmp, 0, 0, bmp.Width, bmp.Height); [Drawingthe >> >bmp >> >> >in 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.DrawImageUnscaled. 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 andcopy >to >> >> >clipboard, it is exactly same as yours. But it doesn't work. >> >> > >> >> >System.Drawing.Bitmap TheClippedBmp = new >> >Bitmap((int)this.ImageRect.Width, >> >> >(int)this.ImageRect.Height); >> >> > >> >> >Graphics Gra = Graphics.FromImage(TheClippedBmp); >> >> > >> >> >Gra.DrawImageUnscaled(this.TheImagetoDraw, (int) ImageRect.X ,(int) >> >> >ImageRect.Y,(int) ImageRect.Width,(int) ImageRect.Height); >> >> > >> >> >System.Windows.Forms.Clipboard.SetDataObject(TheC lippedBmp) ; >> >> > >> >> >Gra.Dispose() ; >> >> > >> >> >TheClippedBmp.Dispose() ; >> >> > >> >> >this.statusBarPanelIBMsg.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 cropthe >> >> 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 >> >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@*NOSPAM*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 agif >> >> >> file, copies a 30 x 30 portion ( rect(0, 0, 29, 29) of theoriginal >> >> >> into the new bitmap, and draws both to a picturebox to ensurethat 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**********@hotmail.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 Ineed >> >some >> >> >> >help. >> >> >> > >> >> >> >Any suggestions please? >> >> >> > >> >> >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrotein >> >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 youdoing. >> >> >> >> >> >> >> >> Jonathan Schafer >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> On Mon, 14 Jul 2003 09:11:54 -0700, "Anand Ganesh" >> >> >> >> <an**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >Hi, >> >> >> >> > >> >> >> >> >See you have a .gif image. I have .tif image. May be that isthe >> >> >problem. >> >> >> >> >Also I am not sure how to get another image itself; insteadof >> >drawing >> >> >to >> >> >> >> >the user, I should get a portion of the image and save in a >file. >> >> >> >> > >> >> >> >> >Regards >> >> >> >> >Anand Ganesh >> >> >> >> > >> >> >> >> >"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com>wrote >in >> >> >message >> >> >> >> >news:e9********************************@4ax.com.. . >> >> >> >> >> The following code works for me. It loads a .gif, drawsthe >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.Drawing.Image.FromFile(@"C:\cygwi n\bin\openssl-0.9.6d\doc\ope >n >> >s >> >> >s >> >> >> >l >> >> >> >> >_button.gif"); >> >> >> >> >> Graphics g1 = pictureBox1.CreateGraphics(); >> >> >> >> >> g1.DrawImage(img, 0, 0, img.Width, img.Height); >> >> >> >> >> g1.Dispose(); >> >> >> >> >> Graphics g3 = Graphics.FromImage(bmp); >> >> >> >> >> g3.DrawImageUnscaled(img, 0, 0, bmp.Width, bmp.Height); >> >> >> >> >> Graphics g2 = pictureBox2.CreateGraphics(); >> >> >> >> >> g2.DrawImageUnscaled(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**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >> >> >I tried this but I got an error >> >> >> >> >> > >> >> >> >> >> > >> >> >> >> >> >An unhandled exception of type 'System.Exception' occurredin >> >> >> >> >> >system.drawing.dll >> >> >> >> >> > >> >> >> >> >> >Additional information: A Graphics object cannot becreated >from >> >an >> >> >> >image >> >> >> >> >> >that has an indexed pixel format. >> >> >> >> >> > >> >> >> >> >> > >> >> >> >> >> >But the problem is after clipping I need another Imageobject >> >> >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@*NOSPAM*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 specifythe >> >source >> >> >> >and >> >> >> >> >> >> dest rects. >> >> >> >> >> >> >> >> >> >> >> >> Jonathan Schafer >> >> >> >> >> >> >> >> >> >> >> >> On Fri, 11 Jul 2003 18:49:11 -0700, "Anand Ganesh" >> >> >> >> >> >> <an**********@hotmail.com> wrote: >> >> >> >> >> >> >> >> >> >> >> >> >HI All, >> >> >> >> >> >> > >> >> >> >> >> >> >I have an Image. I want to clip a portion of it andcopy >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 >> >> >> >> >> >> > >> >> >> >> >> >> >> >> >> >> >> > >> >> >> >> >> >> >> >> >> > >> >> >> >> >> >> >> > >> >> >> >> >> > >> >> >> > >> > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |