trying to save image after modification

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

does anyone know why this source code throws an error??


Dim myBitmap As System.Drawing.Bitmap
Dim myGraphics As Graphics
myBitmap = New System.Drawing.Bitmap(fileName:="C:\test.tif")
myGraphics = PictureBox1.CreateGraphics
Dim expansionRectangle As New Rectangle(25, 550, 1000, 350)
Dim destRectangle1 As New Rectangle(0, 0, 1000, 350)
myGraphics.DrawImage(myBitmap, destRectangle1, expansionRectangle,
GraphicsUnit.Pixel)
PictureBox1.Image.Save("c:\xx.tif")
myGraphics.Dispose()
myBitmap.Dispose()
myBitmap = Nothing
myGraphics = Nothing
 
does anyone know why this source code throws an error??


Dim myBitmap As System.Drawing.Bitmap
Dim myGraphics As Graphics
myBitmap = New System.Drawing.Bitmap(fileName:="C:\test.tif")
myGraphics = PictureBox1.CreateGraphics
Dim expansionRectangle As New Rectangle(25, 550, 1000, 350)
Dim destRectangle1 As New Rectangle(0, 0, 1000, 350)
myGraphics.DrawImage(myBitmap, destRectangle1, expansionRectangle,
GraphicsUnit.Pixel)
PictureBox1.Image.Save("c:\xx.tif")
myGraphics.Dispose()
myBitmap.Dispose()
myBitmap = Nothing
myGraphics = Nothing
What error does it throw??
 
Joe said:
does anyone know why this source code throws an error??


Dim myBitmap As System.Drawing.Bitmap
Dim myGraphics As Graphics
myBitmap = New System.Drawing.Bitmap(fileName:="C:\test.tif")
myGraphics = PictureBox1.CreateGraphics

That doesn't get a graphics object for the image in the picture box, it
gets a graphics object for the screen. There is no specific
CreateGraphics method for the picture box control, you are using the
method inherited from the Control class.
Dim expansionRectangle As New Rectangle(25, 550, 1000, 350)
Dim destRectangle1 As New Rectangle(0, 0, 1000, 350)
myGraphics.DrawImage(myBitmap, destRectangle1, expansionRectangle,
GraphicsUnit.Pixel)
PictureBox1.Image.Save("c:\xx.tif")

Have you even put an image in the picture box? There is no code for it here.
 
That doesn't get a graphics object for the image in the picture box, it
gets a graphics object for the screen. There is no specific
CreateGraphics method for the picture box control, you are using the
method inherited from the Control class.


Have you even put an image in the picture box? There is no code for it here.

yes, the picture is displayed.
ok, maybe I am little confused; I am just trying to load TIF file
(whatever object it may be in), select some rectangle area and copy it
and save it into separate TIFF file.
how hord is it to do?


thanks for your help.
 
Joe said:
yes, the picture is displayed.

Yes, of course it's displayed. You are drawing it on the screen where
the picture box happens to be. It doesn't change anything about the
picture box, though.
ok, maybe I am little confused; I am just trying to load TIF file
(whatever object it may be in), select some rectangle area and copy it
and save it into separate TIFF file.
how hord is it to do?

Just create another Bitmap object, create a Graphics object for it and
use that to draw on the image. Then you can save it.
 
Yes, of course it's displayed. You are drawing it on the screen where
the picture box happens to be. It doesn't change anything about the
picture box, though.


Just create another Bitmap object, create a Graphics object for it and
use that to draw on the image. Then you can save it.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

thanks Goran, but how can I select rectangle from the first Bitmap
object, copy it content and paste it to the second bitmap box?

any ideas?
 
Joe said:
thanks Goran, but how can I select rectangle from the first Bitmap
object, copy it content and paste it to the second bitmap box?

any ideas?

Do as you did in the original code. Just forget about the picture box
and use a Bitmap object.
 
Do as you did in the original code. Just forget about the picture box
and use a Bitmap object.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -


I tried; it gives "unknown error":

' open test and cut a slice
Dim myBitmap As New Bitmap("C:\TEST.TIF")
Dim expansionRectangle As New Rectangle(135, 10,
myBitmap.Width, myBitmap.Height)

Dim myGraphics As Graphics
Dim image As Image
Dim point As Point

myGraphics.DrawImage(myBitmap, expansionRectangle)
myGraphics.Save()
myGraphics.Dispose()

' create another bitmap
Dim bitmap As Bitmap
bitmap = New Bitmap(100, 100)
Dim g As Graphics
' copy slice from 1. to 2.
g = Graphics.FromImage(bitmap)
g.DrawImage(myBitmap, expansionRectangle)
g.Dispose()
' save to disk as 111.tif
bitmap.Save("c:\111.tif")
 
Joe said:
I tried; it gives "unknown error":

The complete error message would be helpful. That would tell where the
error message is coming from and where it occured.
' open test and cut a slice
Dim myBitmap As New Bitmap("C:\TEST.TIF")
Dim expansionRectangle As New Rectangle(135, 10,
myBitmap.Width, myBitmap.Height)

So far so good.
Dim myGraphics As Graphics
Dim image As Image
Dim point As Point

myGraphics.DrawImage(myBitmap, expansionRectangle)
myGraphics.Save()
myGraphics.Dispose()

What's this code doing here? You haven't created any Image object or any
Graphics object, so you are just working with null references.
' create another bitmap
Dim bitmap As Bitmap
bitmap = New Bitmap(100, 100)
Dim g As Graphics
' copy slice from 1. to 2.
g = Graphics.FromImage(bitmap)
g.DrawImage(myBitmap, expansionRectangle)
g.Dispose()
' save to disk as 111.tif
bitmap.Save("c:\111.tif")

That looks ok. Just remove that code in the middle, and it should work
just fine.

Don't forget to dispose the bitmaps.
 
That looks ok. Just remove that code in the middle, and it should work
just fine.

Don't forget to dispose the bitmaps.

here's the code:

Dim myBitmap As New Bitmap("C:\TEST.TIF")
Dim expansionRectangle As New Rectangle(135, 10,
myBitmap.Width, myBitmap.Height)

Dim bitmap As Bitmap
bitmap = New Bitmap(100, 100)
Dim g As Graphics

g = Graphics.FromImage(bitmap)
g.DrawImage(myBitmap, expansionRectangle)
g.Dispose()

bitmap.Dispose()

bitmap.Save("c:\111.tif",
System.Drawing.Imaging.ImageFormat.Tiff)


and error comes from the last line: bitmap.Save("c:\111.tif") ; it is
"Paremeter is not Valid."


any ideas?? (I've tried with Jpeg file; it is not a tiff/jpeg bitmap
format problem)
 
Joe said:
here's the code:

Dim myBitmap As New Bitmap("C:\TEST.TIF")
Dim expansionRectangle As New Rectangle(135, 10,
myBitmap.Width, myBitmap.Height)

Dim bitmap As Bitmap
bitmap = New Bitmap(100, 100)
Dim g As Graphics

g = Graphics.FromImage(bitmap)
g.DrawImage(myBitmap, expansionRectangle)
g.Dispose()

bitmap.Dispose()

You can't save the image after you have disposed it.
 
Joe said:
it's saving the empty, black box :(

Why are you drawing the image at the coordinates 135,10? That's outside
the 100x100 bitmap that you have created.
 
Back
Top