.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

How to Convert System.Drawing.Image Variable to Byte variable

Posted By:      Posted Date: October 07, 2010    Points: 0   Category :ASP.Net


I got a variable of type System.Drawing.Image and need to convert it to a variable of type byte so I can store the image in the database. Can someone show me how to do that in VB.NET code.

Thanks alot

View Complete Post

More Related Resource Links

System.Drawing.Image.Fromfile is not working


I am trying to get the height and width of the image dynamically.  The reason is I am loading multiple images using code, where the image name is from the database. 

Here is my code:

in the ascx page, I have an asp image

<asp:image id="imgInfo" runat="server" /> 


In the code behind

string ImagePath = "~/products/img/" + dataviewImgInfo[0]["imgName"].ToString();

System.Drawing Image imgProd = System.Drawing.Image.FromFile(ImagePath);

if(imgProd.Height > 200)

imgInfo.Height = 100;




imgInfo.Height = 200



WHen I run this page I am getting the folloiwng error

Displaying a System.Drawing.Bitmap in a WPF Image control

How would I convert a System.Drawing.Btimap into a comparable WPF image control so I can display in on my WPF window? Thanks

Converting a System.Windows.Control.Image object to byte array.

Hi i need to convert a specific System.Windows.Control.Image object into an (int) array, because i'm trying to convert a silverlight application into a wpf project. private void createGi02GradBrush()<br/> {<br/> WriteableBitmap oWriteableBitmap = new WriteableBitmap(256, 256);<br/> Image oImage = new Image();<br/> oImage.Arrange(new Rect(0, 0, 256, 256));<br/> oImage.Measure(new Size(256, 256));<br/> oImage.Source = new WriteableBitmap(256, 256);<br/> Gi02GradShaderEffect oGi02GradShaderEffect = new Gi02GradShaderEffect();<br/> oGi02GradShaderEffect.pArray = oPImageBrush;<br/> oGi02GradShaderEffect.gi1GradArray = oGi1GradImageBrush;<br/> oImage.Effect = oGi02GradShaderEffect;<br/> oWriteableBitmap.Render(oImage, null);//here<br/> oWriteableBitmap.Invalidate(); <br/> this.oGi02GradImageBrush = new ImageBrush();<br/> this.oGi02GradImageBrush.ImageSource = oWriteableBitmap;<br/> } I've found some similar topics however Image object is not the same and most of its methods aren't available. Here I'm rendering a writablebitmap with a given Image object, but in wpf we need an array. oWriteableBitmap.Render(oImage, null); Any idea? Thanks. Joseph.  

How to convert string variable to url?



 I need to convert a string to http url.

The way i have to do this is--I have to retriew a value from database and convert it into a sharable http url in Asp.net C#.


I am new to this concept,Can anyone help me.......................

System.Drawing.Image.FromFile crashes for tiff files on 64bit system


The following pice of code runs fine on my development machine (win7, 32bit) but ends with Exception on the server (Win2008R2, 64bit).


        static void Main(string[] args)


            Image image = Image.FromFile(args[0]);





The exception will be thrown for TIFF files (from other sources) but ie not for PNG files.

I was wondering why the test program above works fine on my Win 32bit machine with .NET 4 but not on my 64bit machine with .NET 4.

Any help appreciated.



Error: The variable System::ErrorDescription contains a string that exceeds the maximum allowed leng


i get the following error message, when I try to email the error description, by invoking a 'Send Mail Task' in OnError event.

"Error: The variable System::ErrorDescription contains a string that exceeds the maximum allowed length of 4000 characters."

Any idea how to resolve this issue? I am using substring when I build the expression. But still am facing this issue.

Failing Connection String Environmental Variable, XML Variable, System Variable (SQL Server) Setup


We attempted to implement the Chapter 2 Framework from the Knight, Vermeen, Moss, Hackney book on SSIS 2008: Problem Design Solution. We see a couple of problem areas (at least) in our interpretation of the guidelines from the book.

1. We are attempting to have one connection string per server: dev/prod

2. We have set up environmental variables per server: dev/prod

3. We have set up XML Package Variables per server: dev/prod

4. We have set up System Variables per server in SQL Server: dev/prod

Problem 1:

We've had to manually key in the common connection string to all packages on a server environment in the component properties area (control flow; not data flow--ok there)

Shouldn't the connection property find the connection that we've instantiated and provide us with a pull down to select it?

The components are un-editable after we select the common connection string, but they still run properly in our SQL Server Agent Job.

Problem 2:

The dev server variable overlays the prod server variable information duringprod component editing

We are'nt sure if these problems are related.

Here is our package variable:



Here is our DTSconfig file; we have a few variations of this that we've tried.

Get Image from HTTP in a System.Drawing.Image


How can i get a Image that exitsts on the WWW, into my project.
Normally i get a image on this way:


        System.Drawing.Image ImgWatermerk = System.Drawing.Image.FromFile(PNGPath);


But right now the image is remote like http://www.google.nl/google.jpg


How to convert image class object to byte type in order to insert database


I am trying to resize images after uploading and than inserting them into my database.

Dim newMainImage As System.IO.Stream = System.IO.Stream.Null
resizedMainImage.Save(newMainImage, jgpEncoder, myEncoderParameters)
Dim newMain As Image = Image.FromStream(newMainImage)

I was able to resize it however I couldnt find a way to convert image object to byte() in order to

I need something like

Dim ImageContent(newMain.ContentLength - 1) As Byte

newMain.InputStream.Read(ImageContent, 0 , newMain.ContentLength)

I need to convert image object to a file type in order to get its contentlenght and use .inputStream.Read function. Or is there any other way?

I have got a byte array and want to convert it into a image

Byte[] photo = Convert.FromBase64String(value);
//old 1
MemoryStream ms = new MemoryStream(photo, 0, photo.Length);
ms.Position = 0;
ms.Write(photo, 0, photo.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);// this line giving exception parameter not valid


System.Drawing.Image.FromStream() ArgumentException: Parameter not valid.


I am getting an ArgumentException: Parameter is not valid.

Does anyone know what's wrong with my code here?

        ImgService obj = new ImgService();

        byte[] data = obj.ShowImg(6);

        Int32 offset = 78;

        MemoryStream memStream = new MemoryStream();

        memStream.Write(data, offset, data.Length - offset);

        System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);

        img.Save(Server.MapPath("image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);


        Image1.ImageUrl = Server.MapPath("image.jpg");


        ImgService obj = new ImgService();

        byte[] data = obj.ShowImg(6);
        Int32 offset = 78;

System.Drawing.Image.FromStream() get a Parameter is not valid


Anyone help fix Parameter is not valid errors , when i tried rename and save a file using fileupload to server

my code here :

if (fupImage.PostedFile.FileName != "")
                if (!Utitlties.Instance.CheckImageFileType(fupImage.PostedFile.FileName))
                    lblMessageForfupImage.Visible = true;
                    lblMessageForfupImage.Text = "Image file only accept *.jpg, *.png, *.bmp, *.gif Extensions !";
                    evt.ImageURL = Utils.Instance.SaveImage(fupImage.PostedFile.InputStream, fupImage.PostedFile.FileName, true);

anh here is methos SaveImage :


public string SaveImage(Stream stream, string filename, bool IsImageFile/*, bool CreateThumImage*/)
        string ImgUrl = string.Empty;
        string d = Utitlties.Instance.MakeFolderName();
        System.Drawing.Image img;
        string dirToSave = HttpContext.Current.Server.MapPath("Images/Uploads") + "/" + d + "/";
        string dir = "Images/Uploads/" + d + "/";
            if (IsImageFile == true)

Passing a System::Object^ from a returned variable


I have a singleton class thet has a function A^ A::Instance();

I'm trying to pass this to an EventHandler as an object and I get the folowing error

Error 4 error C2664: 'System::EventHandler::Invoke' : cannot convert parameter 1 from 'A^(__clrcall *)(void)' to 'System::Object ^'

for  example I have an event

public event EventHandler TestEvent

and then I'm trying to call TestEvent(A::Instance, nullptr);




Local variable scoping in C#

In my previous post, Compiler-generated scopes for local variable declarations, I briefly touched on the issue of multiple meanings applied to the same name. In this post, I'll aim to flush out the compiler's rules with regards to binding names in their local scopes.

Compiler-generated scopes for local variable declarations

I was tasked with understanding and fixing a bug on error reporting with foreach iteration variables the other day, and it got me thinking about local variable scoping rules in C# in general. First, the bug.

Retrieving a variable from a previous page



I'm looking to have a search page where a user will select an item from a listview, then then taken to another page that wil display data based on their original selection.

A user will type in a string and be presented with a list of usernames that are like that string, when they select a user name who's profile they would like to view I would like the page that they're taken to, to be that users profile.

How would I transfer the variable from the search page to the profile page, and when I work on the profile page, how can I access that variable?


Embeb a variable number of videos


I need to provide some alternate content for my non-Flash users.

I have a directory of video files that is scanned using the DirectoryInfo() function, and I want to use the results to embed all files ending in ".mov" in QuickTime players on a web form.

How would you approach this? Right now I am using the count on the array of videos to determine how many times to repeat the embed code.  But this seems inelegant.

Is there a way to have a single QuickTime player.ascx control that gets duplicated according to the number of videos available for display?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend