How to retrieve BinaryCode from SQL Server to PictureBox
I have a database on my private host, which contains a column for inserting pictures. I completely converted my picture from PictureBox to Binary code and when I go to the database, I can see the binary codes properly. I also programmed a function for converting binary codes to image, and set an event handler for user to click on a cell in DataGridView and all of the values will be displayed on the specific controls.
First I got an error and that error was something like this :
Cannot convert from object to byte
I solved this problem by programming a function and convert object binary to byte, but now, when I try to convert byte
(binary array) to image, I face an error and I cannot solve this problem. and problem is :
Parameter is invalid.
Here is my code:
Converting Image to Binary
byte ConvertImageToBinary(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
Convert Object to Binary
byte ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
Convert Binary to Image
Image ConvertBinaryToImage(byte data)
{
using (MemoryStream ms = new MemoryStream(data))
{
return Image.FromStream(ms);
}
}
And at last, here is my code in
private void DataCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e):
if (DataCustomer.CurrentRow.Cells[9].Value == null)
PictureCustomer.Image = null;
else
{
byte ImageByte = ObjectToByteArray(DataCustomer.CurrentRow.Cells[9].Value);
PictureCustomer.Image = ConvertBinaryToImage(ImageByte);
}
Any helpful suggestion would be appreciate.
Thanks in advance
c# windows-forms-designer
|
show 3 more comments
I have a database on my private host, which contains a column for inserting pictures. I completely converted my picture from PictureBox to Binary code and when I go to the database, I can see the binary codes properly. I also programmed a function for converting binary codes to image, and set an event handler for user to click on a cell in DataGridView and all of the values will be displayed on the specific controls.
First I got an error and that error was something like this :
Cannot convert from object to byte
I solved this problem by programming a function and convert object binary to byte, but now, when I try to convert byte
(binary array) to image, I face an error and I cannot solve this problem. and problem is :
Parameter is invalid.
Here is my code:
Converting Image to Binary
byte ConvertImageToBinary(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
Convert Object to Binary
byte ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
Convert Binary to Image
Image ConvertBinaryToImage(byte data)
{
using (MemoryStream ms = new MemoryStream(data))
{
return Image.FromStream(ms);
}
}
And at last, here is my code in
private void DataCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e):
if (DataCustomer.CurrentRow.Cells[9].Value == null)
PictureCustomer.Image = null;
else
{
byte ImageByte = ObjectToByteArray(DataCustomer.CurrentRow.Cells[9].Value);
PictureCustomer.Image = ConvertBinaryToImage(ImageByte);
}
Any helpful suggestion would be appreciate.
Thanks in advance
c# windows-forms-designer
theBinaryFormatter
does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored inDataCustomer.CurrentRow.Cells[9].Value
?
– dlatikay
Nov 24 '18 at 21:45
You have aMemoryStream
in ausing
block. You shouldreturn (Image)Image.FromStream(ms).Clone();
. TheMemoryStream
is being disposed
– Jimi
Nov 25 '18 at 1:05
@dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image.
– Ali Shahbazi
Nov 25 '18 at 7:02
@Jimi I did what you've said, but nothing has changed. I still have that problem.
– Ali Shahbazi
Nov 25 '18 at 7:23
get rid of thatBinaryFormatter
. make sure the picture is stored asbyte
. could you show the code where you assign that value to the gridview's cell? is it databound to a db table?
– dlatikay
Nov 25 '18 at 8:00
|
show 3 more comments
I have a database on my private host, which contains a column for inserting pictures. I completely converted my picture from PictureBox to Binary code and when I go to the database, I can see the binary codes properly. I also programmed a function for converting binary codes to image, and set an event handler for user to click on a cell in DataGridView and all of the values will be displayed on the specific controls.
First I got an error and that error was something like this :
Cannot convert from object to byte
I solved this problem by programming a function and convert object binary to byte, but now, when I try to convert byte
(binary array) to image, I face an error and I cannot solve this problem. and problem is :
Parameter is invalid.
Here is my code:
Converting Image to Binary
byte ConvertImageToBinary(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
Convert Object to Binary
byte ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
Convert Binary to Image
Image ConvertBinaryToImage(byte data)
{
using (MemoryStream ms = new MemoryStream(data))
{
return Image.FromStream(ms);
}
}
And at last, here is my code in
private void DataCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e):
if (DataCustomer.CurrentRow.Cells[9].Value == null)
PictureCustomer.Image = null;
else
{
byte ImageByte = ObjectToByteArray(DataCustomer.CurrentRow.Cells[9].Value);
PictureCustomer.Image = ConvertBinaryToImage(ImageByte);
}
Any helpful suggestion would be appreciate.
Thanks in advance
c# windows-forms-designer
I have a database on my private host, which contains a column for inserting pictures. I completely converted my picture from PictureBox to Binary code and when I go to the database, I can see the binary codes properly. I also programmed a function for converting binary codes to image, and set an event handler for user to click on a cell in DataGridView and all of the values will be displayed on the specific controls.
First I got an error and that error was something like this :
Cannot convert from object to byte
I solved this problem by programming a function and convert object binary to byte, but now, when I try to convert byte
(binary array) to image, I face an error and I cannot solve this problem. and problem is :
Parameter is invalid.
Here is my code:
Converting Image to Binary
byte ConvertImageToBinary(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
Convert Object to Binary
byte ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
Convert Binary to Image
Image ConvertBinaryToImage(byte data)
{
using (MemoryStream ms = new MemoryStream(data))
{
return Image.FromStream(ms);
}
}
And at last, here is my code in
private void DataCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e):
if (DataCustomer.CurrentRow.Cells[9].Value == null)
PictureCustomer.Image = null;
else
{
byte ImageByte = ObjectToByteArray(DataCustomer.CurrentRow.Cells[9].Value);
PictureCustomer.Image = ConvertBinaryToImage(ImageByte);
}
Any helpful suggestion would be appreciate.
Thanks in advance
c# windows-forms-designer
c# windows-forms-designer
edited Nov 24 '18 at 20:51
Ali Shahbazi
asked Nov 24 '18 at 20:24
Ali ShahbaziAli Shahbazi
63
63
theBinaryFormatter
does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored inDataCustomer.CurrentRow.Cells[9].Value
?
– dlatikay
Nov 24 '18 at 21:45
You have aMemoryStream
in ausing
block. You shouldreturn (Image)Image.FromStream(ms).Clone();
. TheMemoryStream
is being disposed
– Jimi
Nov 25 '18 at 1:05
@dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image.
– Ali Shahbazi
Nov 25 '18 at 7:02
@Jimi I did what you've said, but nothing has changed. I still have that problem.
– Ali Shahbazi
Nov 25 '18 at 7:23
get rid of thatBinaryFormatter
. make sure the picture is stored asbyte
. could you show the code where you assign that value to the gridview's cell? is it databound to a db table?
– dlatikay
Nov 25 '18 at 8:00
|
show 3 more comments
theBinaryFormatter
does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored inDataCustomer.CurrentRow.Cells[9].Value
?
– dlatikay
Nov 24 '18 at 21:45
You have aMemoryStream
in ausing
block. You shouldreturn (Image)Image.FromStream(ms).Clone();
. TheMemoryStream
is being disposed
– Jimi
Nov 25 '18 at 1:05
@dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image.
– Ali Shahbazi
Nov 25 '18 at 7:02
@Jimi I did what you've said, but nothing has changed. I still have that problem.
– Ali Shahbazi
Nov 25 '18 at 7:23
get rid of thatBinaryFormatter
. make sure the picture is stored asbyte
. could you show the code where you assign that value to the gridview's cell? is it databound to a db table?
– dlatikay
Nov 25 '18 at 8:00
the
BinaryFormatter
does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored in DataCustomer.CurrentRow.Cells[9].Value
?– dlatikay
Nov 24 '18 at 21:45
the
BinaryFormatter
does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored in DataCustomer.CurrentRow.Cells[9].Value
?– dlatikay
Nov 24 '18 at 21:45
You have a
MemoryStream
in a using
block. You should return (Image)Image.FromStream(ms).Clone();
. The MemoryStream
is being disposed– Jimi
Nov 25 '18 at 1:05
You have a
MemoryStream
in a using
block. You should return (Image)Image.FromStream(ms).Clone();
. The MemoryStream
is being disposed– Jimi
Nov 25 '18 at 1:05
@dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image.
– Ali Shahbazi
Nov 25 '18 at 7:02
@dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image.
– Ali Shahbazi
Nov 25 '18 at 7:02
@Jimi I did what you've said, but nothing has changed. I still have that problem.
– Ali Shahbazi
Nov 25 '18 at 7:23
@Jimi I did what you've said, but nothing has changed. I still have that problem.
– Ali Shahbazi
Nov 25 '18 at 7:23
get rid of that
BinaryFormatter
. make sure the picture is stored as byte
. could you show the code where you assign that value to the gridview's cell? is it databound to a db table?– dlatikay
Nov 25 '18 at 8:00
get rid of that
BinaryFormatter
. make sure the picture is stored as byte
. could you show the code where you assign that value to the gridview's cell? is it databound to a db table?– dlatikay
Nov 25 '18 at 8:00
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462054%2fhow-to-retrieve-binarycode-from-sql-server-to-picturebox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462054%2fhow-to-retrieve-binarycode-from-sql-server-to-picturebox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
the
BinaryFormatter
does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored inDataCustomer.CurrentRow.Cells[9].Value
?– dlatikay
Nov 24 '18 at 21:45
You have a
MemoryStream
in ausing
block. You shouldreturn (Image)Image.FromStream(ms).Clone();
. TheMemoryStream
is being disposed– Jimi
Nov 25 '18 at 1:05
@dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image.
– Ali Shahbazi
Nov 25 '18 at 7:02
@Jimi I did what you've said, but nothing has changed. I still have that problem.
– Ali Shahbazi
Nov 25 '18 at 7:23
get rid of that
BinaryFormatter
. make sure the picture is stored asbyte
. could you show the code where you assign that value to the gridview's cell? is it databound to a db table?– dlatikay
Nov 25 '18 at 8:00