Results 1 to 5 of 5

Thread: pictuer app problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Angry pictuer app problem

    I am doing a picture app and I use opendialogbox and memorystream but cf does not support (Fromfile when I use the opendialogbox) and also does not support (FromStream when memorystream is used. Could any one help on how I can go about this. Thanx

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: pictuer app problem

    What exactly are you trying to do - I am confused by
    use opendialogbox and memorystream but cf does not support (Fromfile when I use the opendialogbox)
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: pictuer app problem

    Hey there,

    Either of the following techniques should work just fine:

    Code:
    pictureBox1.Image = new Bitmap (Full.Path.To.Image);
    or:

    Code:
    private void LoadImage(string filePath)
    {
    	using (FileStream reader = new FileStream(filePath, FileMode.Open))
    	{
    		byte[] data = new byte[reader.Length];
    		reader.Read(data, 0, (int)reader.Length);
    		using (MemoryStream memory = new MemoryStream(data))
    		{
    			pictureBox1.Image = new Bitmap(memory);
    			pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    		}
    	}
    }
    Hope that helps!!

    Gary

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Re: pictuer app problem

    Thanks Gary. You code has help me to open the images from file but, I am working on saving the images in a db. I am a bit new to compact framework so could you help out on the code below. The first display the image in the picturebox which has already populated in a dataset and bindingsource. Is from my pc code which works fine but how to modify it to work with cf.

    Code:
      Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
            ' When nothing is selected in the ListBox, the SelectedIndex = -1.
            If lstPictures.SelectedIndex < 0 Then
                MessageBox.Show("There are no images in the database to display.", _
                    "Empty Database!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
              
                ' calling Image.FromStream().
                Dim arrPicture() As Byte = _
                 CType(Bindingsource.item(lstPictures.SelectedIndex)("Picture"), _
                 Byte())
                Dim ms As New MemoryStream(arrPicture)
    
                With PictureBox2
                    .Image = Image.FromStream(ms)
                    .SizeMode = PictureBoxSizeMode.CenterImage
                    .BorderStyle = BorderStyle.Fixed3D
                End With
    
                lblFileName.Text = _
                 dsPictures.Tables(0).Rows(lstPictures.SelectedIndex)("FileName").ToString
    
                ' Close the stream object to release the resource.
                ms.Close()
            End If
        End Sub
    Compact frmaework does not support image.fromstream(ms), so if I use new bitmap(ms) I get error of not enough memory.

    Code two save the image to the db but cf does not support RawFormat, so I use clone and it does not do anything.
    Code:
        Private Sub Adding()
            Dim arrFilename As String = Me.txtfileName.Text
    
            Dim ms As New MemoryStream()
            PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
            Dim arrImage() As Byte = ms.GetBuffer
            ms.Close()
            Dim cmd As New SqlCeCommand
            cmd.CommandText = _
                "INSERT INTO Pictures (Picture, PictureName)" & _
                "VALUES (@Picture, @Filename)"
            cmd.Connection = sCon
            With cmd.Parameters
                .AddWithValue("@Picture", arrImage)
                .AddWithValue("@FileName", arrFilename)
            End With
    
            Try
                sCon.Open()
                cmd.ExecuteNonQuery()
    
                MessageBox.Show(Me.txtFileName.Text & " saved to the database.", _
                    "Image Save Status", MessageBoxButtons.OK, _
                    MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
    
            Catch sqlExc As SqlCeException
                MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            End Try
            sCon.Close()
        End Sub
    I would be grateful if you could help on how to modify the above to codes to get them work with cf. Thanx very much
    Last edited by wiadus; Sep 23rd, 2008 at 06:13 AM.

  5. #5
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: pictuer app problem

    HI,
    courtesy of Darren Schaffer

    Code:
    MemoryStream ms = new MemoryStream();
       pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
       byte[] imageBytes = ms.ToArray();
    
       SqlCeConnection cn = DatabaseManager.GetInstance().DBConnection;
       string sql = "INSERT INTO Photos (ID, Photo) VALUES (NewID(), ?)";
       SqlCeCommand command = new SqlCeCommand(sql, cn);
     
    
       command.Parameters.Add("@image", SqlDbType.Image, imageBytes.Length);
    
       command.Parameters["@image"].Value = imageBytes;
       
       command.ExecuteNonQuery();
    This code should show you how to go the other way
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width