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(TheClippedBmp) ;
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:(E-Mail Removed)...
> See embedded comments below:
>
> Jonathan Schafer
>
> On Mon, 14 Jul 2003 23:49:48 -0700, "Anand Ganesh"
> <(E-Mail Removed)> 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
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(TheClippedBmp) ;
> >
> >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:(E-Mail Removed)...
> >> 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"
> >> <(E-Mail Removed)> 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:(E-Mail Removed)...
> >> >> 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"
> >> >> <(E-Mail Removed)> 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:(E-Mail Removed)...
> >> >> >> 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\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"
> >> >> >> <(E-Mail Removed)> 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:(E-Mail Removed)...
> >> >> >> >> 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"
> >> >> >> >> <(E-Mail Removed)> 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
> >> >> >> >> >
> >> >> >> >>
> >> >> >> >
> >> >> >>
> >> >> >
> >> >>
> >> >
> >>
> >
>