Click to See Complete Forum and Search --> : pictuer app problem
wiadus
Sep 22nd, 2008, 06:07 AM
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
petevick
Sep 22nd, 2008, 07:10 AM
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)
gep13
Sep 22nd, 2008, 02:01 PM
Hey there,
Either of the following techniques should work just fine:
pictureBox1.Image = new Bitmap (Full.Path.To.Image);
or:
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
wiadus
Sep 23rd, 2008, 06:10 AM
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.
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.
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
petevick
Sep 23rd, 2008, 07:18 AM
HI,
courtesy of Darren Schaffer
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 (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2539562&SiteID=1) should show you how to go the other way
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.