Resize image to store in database - ASP.NET - c#

I followed the code from this guide in order to resize an image before storing it in a database:
https://www.aspsnippets.com/questions/876401/Resize-image-and-save-into-Database-with-Binary-format-using-C-and-VBNet-in-ASPNet/
Here is the code:
protected void Save(object sender, EventArgs e)
{
if (fuImage.HasFile)
{
Byte[] bytes;
string contentType = fuImage.PostedFile.ContentType;
string fileName = Path.GetFileName(fuImage.FileName);
string filePath = fuImage.PostedFile.FileName;
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
// Resize image
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(130, 170, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
using (MemoryStream memoryStream = new MemoryStream())
{
thumbnail.Save(memoryStream, ImageFormat.Png);
bytes = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, (int)bytes.Length);
}
}
// Insert uploaded image to Database
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO tblFiles1 VALUES (#Name, #ContentType, #Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#Name", fileName);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
// Display image after upload to Database
Image1.Visible = true;
byte[] byteData = (byte[])GetData("SELECT Data FROM tblFiles1").Rows[0]["Data"];
string base64String = Convert.ToBase64String(byteData, 0, byteData.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
}
public bool ThumbnailCallback()
{
return false;
}
The problem I am getting however, is once I select an image using the FileUploader and try to run the save method, I get a System.IO.FileNotFoundException at the line:
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
Thanks in advance!

The FileName property is the file name property from the client (https://learn.microsoft.com/en-us/dotnet/api/system.web.httppostedfilebase.filename?view=netframework-4.8#System_Web_HttpPostedFileBase_FileName) so you can't load the file using that property. I can upload an image from c:\yfeyhcdrt\image.png, but this folder will probably not exist on your server. You should load it using the InputStream property instead using https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromstream?view=windowsdesktop-5.0

Related

How to insert and retrieve an image from C# to/from a database? [duplicate]

This question already has answers here:
Byte array to image conversion
(14 answers)
Display image from byte[ ]
(1 answer)
Closed 5 years ago.
I'm new to WPF programming and Microsoft SQL server. I want to insert and retrieve an image to/from a database. I learned about converting an image (Windows.Controls.Image) to byte[] and storing it to a database, but I couldn't convert from byte[] to Image back to display it in a WPF window.
private Image byteArrayToImage(byte[] arr)
{
MemoryStream stream = new MemoryStream();
stream.Write(arr, 0, arr.Length);
stream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(stream); // Exception
BitmapImage returnImage = new BitmapImage();
returnImage.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
returnImage.StreamSource = ms;
returnImage.EndInit();
Image ans = new Image();
ans.Source = returnImage;
return ans;
}
Output:
System.ArgumentException: 'Parameter is not valid.'
private byte[] imageToArray(System.Drawing.Image img) // Work well
{
MemoryStream ms = new MemoryStream();
FileInfo fi = new FileInfo(tempData); // File name
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] pic = ms.ToArray();
return pic;
}
first, create a static class for your PictureHelper. You must import the BitmapImage that is used in WPF, i think its the using System.Drawing.Imaging;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using Application = System.Windows.Forms.Application;
using Size = System.Drawing.Size;
public static class PictureHelper
{
public static BitmapImage GetImage(object obj)
{
try
{
if (obj == null || string.IsNullOrEmpty(obj.ToString())) return new BitmapImage();
#region Picture
byte[] data = (byte[])obj;
MemoryStream strm = new MemoryStream();
strm.Write(data, 0, data.Length);
strm.Position = 0;
Image img = Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
return bi;
#endregion
}
catch
{
return new BitmapImage();
}
}
public static string PathReturner(ref string name)
{
string filepath = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = #"Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.gif;*.png;*.jpg";
openFileDialog.RestoreDirectory = true;
openFileDialog.Title = #"Please select an image file to upload.";
MiniWindow miniWindow = new MiniWindow();
miniWindow.Show();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filepath = openFileDialog.FileName;
name = openFileDialog.SafeFileName;
}
miniWindow.Close();
miniWindow.Dispose();
return filepath;
}
public static string Encryptor(this string safeName)
{
string extension = Path.GetExtension(safeName);
string newFileName = String.Format(#"{0}{1}{2}", Guid.NewGuid(), DateTime.Now.ToString("MMddyyyy(HHmmssfff)"), extension);
newFileName = newFileName.Replace("(", "").Replace(")", "");
return newFileName;
}
public static Bitmap ByteToBitmap(this byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}
public static byte[] BitmapToByte(this Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
}
this is for retrieving the class (in my case, Candidate) with picture
public SortableBindingList<Candidate> RetrieveManyWithPicture(Candidate entity)
{
var command = new SqlCommand { CommandText = "RetrievePictureCandidates", CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("#CandidateId", entity.CandidateId).Direction = ParameterDirection.Input;
DataTable dt = SqlHelper.GetData(command); //this is where I retrieve the row from db, you have your own code for retrieving, so make sure it works.
var items = new SortableBindingList<Candidate>();
if (dt.Rows.Count <= 0) return items;
foreach (DataRow row in dt.Rows)
{
Candidate item = new Candidate();
item.CandidateId = row["CandidateId"].GetInt();
item.LastName = row["LastName"].GetString();
item.FirstName = row["FirstName"].GetString();
item.PictureId = row["PictureId"].GetInt();
item.PhotoType = PictureHelper.GetImage(row["Photo"]); //in my db, this is varbinary. in c#, this is byte[]
items.Add(item);
}
return items;
}
this is for uploading image from wpf to db which I used on my button
private void UploadButton_Click(object sender, EventArgs e)
{
string safeName = "";
string pathName = PictureHelper.PathReturner(ref safeName);
PictureViewModel vm = new PictureViewModel();
if (pathName != "")
{
safeName = safeName.Encryptor();
FileStream fs = new FileStream(pathName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, Convert.ToInt32(fs.Length));
fs.Close();
PicNameLabel.Text = safeName;
vm.Entity.Name = safeName; //this is the byte[]
Bitmap toBeConverted = PictureHelper.ByteToBitmap(data); //convert the picture before sending to the db
vm.Entity.Photo = PictureHelper.BitmapToByte(toBeConverted);
vm.Entity.Path = pathName;
CandidatePictureBox.Image = toBeConverted;
vm.Insert(vm.Entity);
}
}
this is the method to save the picture
public bool Insert(Picture entity)
{
var command = new SqlCommand();
try
{
command.CommandText = "AddPicture";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("#Name", entity.Name).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("#Photo", entity.Photo).Direction = ParameterDirection.Input;
int result = SqlHelper.ExecuteNonQuery(command); //executenonquery will save the params to the db
return true;
}
catch (Exception)
{
return false;
}
}

WPF Byte Array to ImageSource without using MemoryStream

I have an application that creates WPF Image controls on-the-fly to display images from a SQL Server database table. The panels that display the controls get replaced frequently. I use MemoryStreams to allow the controls to access the byte arrays loaded from the database. I cannot close these streams as long as the Image controls are displayed and still have the images displayed. My concern is that these streams will be left open until the Garbage Collector gets around to disposing of the Image controls and hopefully closing the streams too. I will probably end up just making sure that I close the streams or dispose of the Image controls before the panels are replaced but would like to know if there is an issue with leaving these streams open and if there is a way around this.
Here's the code that I am using.
public class ImageToSql
{
private static OpenFileDialog _openFileDialog = new OpenFileDialog();
static ImageToSql()
{
_openFileDialog = new OpenFileDialog();
_openFileDialog.AddExtension = true;
_openFileDialog.CheckFileExists = true;
_openFileDialog.DefaultExt = ".jpg";
_openFileDialog.Filter = "JPEG files (*.jpg)|*.jpg|All files (*.*)|*.*";
_openFileDialog.FilterIndex = 1;
_openFileDialog.Multiselect = false;
}
public static BitmapImage LoadImageFromFile(string caption)
{
BitmapImage bi = new BitmapImage();
_openFileDialog.Title = caption;
if (_openFileDialog.ShowDialog() == true)
{
bi.BeginInit();
bi.StreamSource = File.OpenRead(_openFileDialog.FileName);
bi.EndInit();
}
return bi;
}
public static int StoreImage(string connectionString, string sql, string imageParameterName, BitmapImage bitmapImage)
{
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
sql += ";SELECT ##IDENTITY";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
byte[] data = new byte[bitmapImage.StreamSource.Length];
bitmapImage.StreamSource.Seek(0, SeekOrigin.Begin);
bitmapImage.StreamSource.Read(data, 0, data.Length);
cmd.Parameters.Add(imageParameterName, System.Data.SqlDbType.VarBinary, -1).Value = data;
object obj = cmd.ExecuteScalar();
return Int32.Parse(obj.ToString());
}
}
finally
{
conn.Close();
}
}
public static BitmapImage RetrieveImage(string connectionString, string sql, string imageIDParameterName, int imageID)
{
BitmapImage bi = new BitmapImage();
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(imageIDParameterName, System.Data.SqlDbType.Int).Value = imageID;
byte[] data = (byte[])cmd.ExecuteScalar();
MemoryStream ms = new MemoryStream(data);
ms.Seek(0, SeekOrigin.Begin);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
}
}
finally
{
conn.Close();
}
return bi;
}
}
Below is how this code is called. "_fileImage" and "_sqlImage" are Image controls on my test Window. At one point, I was using a stream to read the file from disk in "LoadImageFromFile" and think I was able to close that stream while still displaying the image in the Image control. So, MemoryStream seems to be special somehow.
BitmapImage bi = ImageToSql.LoadImageFromFile("Select Image to Save");
_fileImage.Source = bi;
int imageID = ImageToSql.StoreImage(connString,
"INSERT INTO PV_Image (Description, Image) VALUES ('Some Image', #pImage)",
"#pImage", bi);
_sqlImage.Source = ImageToSql.RetrieveImage(connString,
"SELECT Image FROM PV_Image WHERE ImageID = #pImageID",
"#pImageID", imageID);
You can dispose the memory stream once it is used using the using pattern:
using (Stream stream = new MemoryStream(data))
{
BitmapImage image = new BitmapImage():
stream.Position = 0;
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
image.Freeze();
return image;
}

how to preview an image without saving into folder

I am Trying to create a social website. If anyone posted an image the it will detect all the faces in the image and draw a square in the face. Then i want to display the image. My problem is how to preview the image without saving it into a folder. Here is My code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
public partial class User_AddPhoto : System.Web.UI.Page
{
private HaarCascade haar;
string base64String;
Stream fs;
protected void Page_Load(object sender, EventArgs e)
{
string location = Server.MapPath("~/Bin/") + "haarcascade_frontalface_default.xml";
haar = new HaarCascade(location);
}
protected void Button1_Click(object sender, EventArgs e)
{
/*Image Preview */
//Session["Image"] = FileUpload1.PostedFile;
//fs = FileUpload1.PostedFile.InputStream;
//BinaryReader br = new BinaryReader(fs);
//byte[] bytes = br.ReadBytes((Int32)fs.Length);
//base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
//Image1.ImageUrl = "data:image/png;base64," + base64String;
/***/
////////////////////////////*Detect Face*///////////////////////////
//DetectFaces();
Bitmap bmpImage = new Bitmap(fs);
Image<Bgr, byte> InputFrame = new Image<Bgr, byte>(bmpImage);
Image<Gray, byte> grayFrame = (InputFrame).Convert<Gray, byte>();
MCvAvgComp[][] faces = grayFrame.DetectHaarCascade(
haar,
1.4,
8,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(20, 20)
);
foreach (MCvAvgComp face in faces[0])
{
InputFrame.Draw(face.rect, new Bgr(Color.Red), 3);
}
Bitmap Display = InputFrame.ToBitmap();
//Display.Save(Server.MapPath("~/Images/aaa.jpg"));
//////////////////////////////////**/////////////////////////////////
/* Photo Inserting Into Folder and Into Database*/
BLUser blUser = new BLUser();
string UserId=Session["UserId"].ToString();
string date = DateTime.Now.ToString("mm-dd_hh_mm_ss");
if (FileUpload1.HasFile)
{
string Extension = Path.GetExtension(FileUpload1.FileName);
string ImageName = date;
string PhotoUrl="~/Images/Posts/" + ImageName + Extension;
FileUpload1.SaveAs(Server.MapPath("~/Images/Posts/" + ImageName + Extension));
blUser.PostImage(PhotoUrl,UserId);
}
/**/
}
}
But i am in stuck to preview the image. Please Help me someone. Thanks In advance :)
to save the image in sql server
Byte[] imgByte = null;
if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
{
HttpPostedFile File = FileUpload1.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
connection = new SqlConnection(ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString.ToString());
connection.Open();
string sql = "INSERT INTO Table1(title,image) VALUES(#theTitle, #theImage) SELECT ##IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("#theTitle", txtTitle.Text);
cmd.Parameters.AddWithValue("#theImage", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblStatus.Text = String.Format("ID is {0}", id);
Image1.ImageUrl = "~/DisplayImg.ashx?id=" + id;
To show the saved image in browser(Handler)
public void ProcessRequest (HttpContext context)
{
Int32 theID;
if (context.Request.QueryString["id"] != null)
theID = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = DisplayImage(theID);
byte[] buffer = new byte[2048];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public Stream DisplayImage(int theID)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
string sql = "SELECT image FROM Table1 WHERE id = #ID";
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#ID", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])theImg);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable {
get
{
return false;
}
}
For more details :http://www.aspnettutorials.com/tutorials/database/saving-retrieving-image-cs/
#john
It is possible to using binary Reader to convert image as a byte data and assign to image source.
refer below link.
http://mehtawebsolution.com/blog/display-image-preview-without-saving-file-to-folder-asp-net/

Browser not displaying base64 images

I want to display images on browser by converting them to base64 string.
Method of conversion is very simple as follows
string convertedfile = Convert.ToBase64String(fileData);
base64 string
<img src="data:image/jpeg;base64,DQoaCgAAAA1JSERSAAAAFAAAABQIBgAAAI2JHQ0AAAABc1JHQgCuzhzpAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAl2cEFnAAAAgAAAAIAAMOExmgAAAAZiS0dEAAAAAAAA+UO7fwAAACJ6VFh0U29mdHdhcmUAAHjaKy8v18vMyy5OTixI1csvSgcANtgGWBBTylwAAADzSURBVDhPY/wPBAxUBExUNAts1KiBlIfoaBhiD8N1NycwHLi/mqgAJhiGp15uYtj1aCbD5OMZDDdenSJsKCjr4QJvfzz977GZAS5t2Mvw//P3j/i0/EeoRlP299/v/667UaX//vv7X6MOpxawCThlw04z/H//8zmGa158fPDfsBq3oYzYSpusW8IMPgJVDEa8gQxff31k+P8PaPE/Bob/fxkYuNkEGfZeXskwZ1cnw6Hm9xhhimHgn/8/Gea9SGP485OR4fu37wx/fzIw/PkBwTA2818OIP8fQ0P4XAY2FjYUQ7G6kHBU4lZBMNmQavjgNxAArnYq4oN7ItkAAAAASUVORK5CYII=" alt="Original File"/>
For some reason my browser is not able to display it. Is there any MIME type I have to set in IIS? or what could be the problem?
Edit:
To write file data to SQL
Stream fs = imgUpload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//string base64String = Convert.ToBase64String(bytes);
//imgPicture.Src = "data:image/png;base64," + base64String;
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString());
conn.Open();
using (SqlCommand cmd = new SqlCommand("update tbEHUsers set photo=#binaryValue where UserID=101", conn))
{
// Replace 8000, below, with the correct size of the field
cmd.Parameters.Add("#binaryValue", SqlDbType.VarBinary, -1).Value = bytes;
cmd.ExecuteNonQuery();
}
conn.Close();
My method to get image data from SQL(column datatype varbinary)
byte[] fileData =null;
using (SqlDataReader rdr = CMD.ExecuteReader(CommandBehavior.SequentialAccess))
{
if (rdr.Read())
{
// For some reason the data being returned is blank
// When I run it in SQL I get data being returned.
fileData = (byte[])rdr.GetValue(0);
using (System.IO.FileStream fs = new System.IO.FileStream("D:\\Testing.jpg", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
{
using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs))
{
bw.Write(fileData);
bw.Close();
}
}
}
rdr.Close();
}
The data that you have isn't a PNG image at all. Exchange it for a real PNG image, and it will work.
Example, a 10x10 red square:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAXSURBVChTY/jPwPCfGDyqEC+mtkKG/wCSw8c5Wcde4gAAAABJRU5ErkJggg==">
Here is your original image in base64 form:
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAUABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAQFBgf/xAAiEAABBAEDBQEAAAAAAAAAAAABAAIDBBEFEkETITFDUbH/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAgP/xAAZEQEBAQADAAAAAAAAAAAAAAAAAQIDERL/2gAMAwEAAhEDEQA/AO/oot+62jW6hALnO2MDjgFx+ngck/pwFUQaxFSuwwyaky7HaeGEtxuilPjDR6ye3JacZJBJbneXM15TdyXpoURFooREQEREH//Z">

Reading Image from SQL Error in C#

I have a SQLite DB I have field called PersonalPic of Type BLOB
this is code in which I save image to SQLite
cm.CommandText = "insert into People(Name,FullName,FatherName,MotherName,NationalID,Mobile,Phone,Birth,Intma,Info,Address,CuAddress,PersonalPic,Pics) values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+dateTimePicker1.Value.ToString("yyyy-MM-dd")+"','"+textBox8.Text+"','"+textBox9.Text+"','"+textBox10.Text+"','"+textBox11.Text+"','"+ConvertToString(personalpic)+"','"+ConvertToString(pic)+"')";
cm.ExecuteNonQuery();
public string ConvertToString(String path)
{
FileStream fs = new FileStream(path,
FileMode.OpenOrCreate, FileAccess.Read);
byte[] rawData = new byte[fs.Length];
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(rawData);
return base64String;
}
It`s saved without no problems when I try to retrieve image it gives me Parameter is no valid Exception
SQLiteDataReader reader = cm.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
textBox1.Text = reader["Name"].ToString();
textBox2.Text = reader["FullName"].ToString();
textBox3.Text = reader["FatherName"].ToString();
textBox4.Text = reader["MotherName"].ToString();
textBox5.Text = reader["NationalID"].ToString();
textBox6.Text = reader["Mobile"].ToString();
textBox7.Text = reader["Phone"].ToString();
dateTimePicker1.Value = DateTime.Parse(reader["Birth"].ToString());
textBox8.Text = reader["Intma"].ToString();
textBox9.Text = reader["Info"].ToString();
byte[] photoarray = (byte[])reader["PersonalPic"];
MemoryStream ms = new MemoryStream(photoarray);
MessageBox.Show(photoarray.Length.ToString());
ms.Position = 0;
pictureBox1.Image = Image.FromStream(ms);
}
}
reader.Close();
Exception is in the Line (pictureBox1.Image = Image.FromStream(ms))
Please Help!!
Aside from the horrendous sql inject insert statement, you're converting your picture byte array to a B64 string. And you're not converting it back on the other side. You're passing complete garbage data to your pictureBox. Either convert it back from byte[] -> B64 string and then decode it back to a byte[].. or just don't save it as a B64 string in the first place
Replace ConvertToString with ConvertToBlob
public byte[] ConvertToBlob(String path)
{
FileStream fs = new FileStream(path,
FileMode.OpenOrCreate, FileAccess.Read);
byte[] rawData = new byte[fs.Length];
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
return rawData;
}
And pass the raw data Byte[] directly to your SQL Query (which you need to rewrite as a parameterised query).

Categories