Yet another save question....

vcvc

Active member
Joined
Nov 14, 2003
Messages
27
Sorry.... this is another how to save question.
I have looked in every forum for a solution, and I think I have tried every solution with no success.
I guess Im a complete idiot or old...same thing I guess.
Im attempting to perform some simple image manipulation using a simple form with simple controls and 2 images. Image on left is unmodified, image on right is the modified "preview". any changes to the control (slider etc) invalidate the form calling onpaint. So far so good - nice simple UI , preview works nice .......... until I try to save. Then I get the same old "my changes are not being saved".
Well...my changes are not being saved.
No, I am not using picture boxes , and yes I am creating a bitmap and writing to that as opposed to the control.
No...I am not drawing silly little lines or arcs etc on the picture, and YES I have looked at every bit of GDI documentation I could find (most of which is junk!).
Please point out my stupid blunder:
Code:
Private Sub Frm_GDI_SUX_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim ArrayPts As Single()() = {New Single() {1, 0, 0, 0, 0}, _
          New Single() {0, 1, 0, 0, 0}, _
          New Single() {0, 0, 1, 0, 0}, _
          New Single() {0, 0, 0, tpVal, 0}, _
          New Single() {0, 0, 0, 0, 1}}
        Dim colorMat As New ColorMatrix(ArrayPts)
        Dim Attrib As New ImageAttributes()
        Attrib.SetColorMatrix(colorMat, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
        Dim ModImg As Image = Image.FromFile(GetFileName(Active_Ctl))
        Dim bmp As New Bitmap(ModImg)
        e.Graphics.FromImage(bmp)
        e.Graphics.DrawImage(bmp, New Rectangle(265, 10, 200, 200), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attrib)
        bmp.Save("c:\GDI_sux.bmp")
End Sub

Please forgive my ignorance as I normally write code that does something bigger than playing with silly little pictures or worse..games.
My wife has requested a small app to manipulate some images...I wisely replied with a "Yes dear"....here we are....
Help me..... the sooner I can get this thing behind me the sonner I can get out of this silly GDI thing,.
 
Last edited by a moderator:
Graphics.FromImage() returns a Graphics object. You need to change the last few lines of code to this:
Code:
Dim bmp As New Bitmap(ModImg)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(265, 10, 200, 200), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attrib)
bmp.Save("c:\blah.bmp")
 
Thanks...but been there done that (but tried it again anyway)
The graphics do not draw, and the save throws "Object reference not set to an instance of an object"

I understand what you are saying and have tried it (and various combinations) countless times.
If I use e.graphics, everything runs, but does not save.
As soon as I create a different graphics object..it blows up.
Makes no sense to me.....

Thanks for trying
 
Can you paste exactly what code gives you the Object Reference Not Set error?
 
The object refence error was my bad...had a stray on my OK button.
However the following still shows no image and saves the original:

Private Sub FrmImgModify_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim curImage As Image = Image.FromFile(GetFileName(Active_Ctl))
e.Graphics.DrawImage(curImage, 10, 10, 200, 200)
Dim ArrayPts As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, tpVal, 0}, New Single() {0, 0, 0, 0, 1}}
Dim colorMat As New ColorMatrix(ArrayPts)
Dim Attrib As New ImageAttributes()
Attrib.SetColorMatrix(colorMat, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
Dim ModImg As Image = Image.FromFile(GetFileName(Active_Ctl))
Dim bmp As New Bitmap(ModImg)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(265, 10, 200, 200), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attrib)
bmp.Save("c:\blah.bmp")
End Sub

Thanks
 
Back
Top