ASP.NET FAQ - GDI+

How can users modify image data using the .NET Framework’s GDI+ and System.Drawing classes?

Here is an article from ftp online: Modify Image Data

Permalink
Why do I get the error message ‘CS0122: ‘System.Drawing.Imaging.ImageFormat.jpeg’ is inaccessible due to its protection level ‘

Try giving the proper file path for the image. Also make sure you have proper permissions on that file.

Permalink
How to align the text on a bitmap

VB.NET

...
Dim sFormat As New StringFormat()
sFormat.Alignment = StringAlignment.Far
...
g.DrawString('Syncfusion', New Font('Arial', 16, FontStyle.Italic), SystemBrushes.WindowText, New PointF(2, 2), sFormat)
......

C#

...
StringFormat sFormat = new StringFormat() ;
sFormat.Alignment = StringAlignment.Far;
...
g.DrawString('Syncfusion', new Font('Arial', 16, FontStyle.Italic), SystemBrushes.WindowText, new PointF(2, 2), sFormat) ;
......
Permalink
How to draw strings vertically on a Bitmap
...
dim sFormat As New StringFormat()  
sFormat.FormatFlags = StringFormatFlags.DirectionVertical  
...
g.DrawString('Syncfusion', new Font('Arial',16,FontStyle.Italic or FontStyle.Bold ),SystemBrushes.WindowText, new PointF(2,2) , sFormat )
...
StringFormat sFormat = new StringFormat()  ;
sFormat.FormatFlags = StringFormatFlags.DirectionVertical  ;
...
g.DrawString('Syncfusion', new Font('Arial',16,FontStyle.Italic|FontStyle.Bold ),SystemBrushes.WindowText, new PointF(2,2) ,sFormat)  ;
...
Permalink
How can I check the image raw format i.e how can I find if Image.RawFormat property returns a ‘jpeg’ or ‘gif’,

VB.NET

if (Img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) then
     	Response.ContentType = 'image/jpeg'
Else 
..
End if

C#

if (Img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) 
{
    	Response.ContentType = 'image/jpeg'
}
else 
{
..
}
Permalink
How to load a image from a website URL

Use WebClient to request it and save it to disk, open the local copy.

Permalink
I get error ‘Value of String cannot be converted System.Drawing.Color’ when I use label1.BackColor= ‘Red’

Try
VB.NET

Label1.BackColor = System.Drawing.Color.FromName('Green')

C#

Label1.BackColor = System.Drawing.Color.FromName('Green');
Permalink
How to show the graphics that have text written on it crisp and clear

Try

VB.NET

objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias

C#

objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
Permalink
How to rotate a image in ASP.NET

VB.NET

Dim img As System.Drawing.Image
Dim strFilename As String = Server.MapPath('curves1.jpg')
img = System.Drawing.Image.FromFile(strFilename)
Dim b = New System.Drawing.Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb)
Dim g As Graphics = Graphics.FromImage(b)
img.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipXY)
g.DrawImage(img, New Point(0, 0))
Response.ContentType = 'image/jpeg'
b.Save(Response.OutputStream, ImageFormat.Jpeg)

C#

System.Drawing.Image img ;   
string strFilename = Server.MapPath('curves1.jpg')  ;
img = System.Drawing.Image.FromFile(strFilename) ; 
Bitmap b = new System.Drawing.Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb)  ;
Graphics  g    = Graphics.FromImage(b)  ;
img.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipXY)  ;
g.DrawImage(img,new Point(0,0))  ;
Response.ContentType='image/jpeg'  ;
b.Save(Response.OutputStream, ImageFormat.Jpeg)  ;
Permalink
How to display image on a browser without using the image tag

VB.NET

Dim objBitmap As New Bitmap(500, 500)
Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
objGraphics.Clear(Color.White)
objGraphics.FillRectangle(New SolidBrush(Color.Purple), 0, 0, 400, 10)
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)

C#

Bitmap objBitmap = new Bitmap(500, 500);
Graphics objGraphics = Graphics.FromImage(objBitmap); 
objGraphics.Clear(Color.White);
objGraphics.FillRectangle(new SolidBrush(Color.Purple  ), 0, 0, 400, 10);
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
Permalink
How can I display image from a Sql Server database

VB.NET

 ’Function
Public Function showImage(empid As Int32) As Byte()
   Dim myconnection As New SqlConnection('Server=localhost;uid=sa;password=;database=northwind;')
   Dim mycommand As New SqlCommand('Select Employeeid, FirstName,Photo from Employees where employeeid =' + empid.ToString(), myconnection)
   myconnection.Open()
   Dim dr As SqlDataReader = mycommand.ExecuteReader()
   dr.Read()
   Dim imgbyte As Byte() = CType(dr('Photo'), Byte())
   Return imgbyte
End Function ’showImage 

’In Page_Load
Dim data As Byte() = showImage(2)
Dim offset As Int32 = 78
Dim mstream As New System.IO.MemoryStream()
mstream.Write(data,offset,data.Length -offset)
Dim bmp As New System.Drawing.Bitmap(mstream)
bmp.Save(Server.MapPath('sample.jpeg'), System.Drawing.Imaging.ImageFormat.Jpeg)
mstream.Close()
Image1.ImageUrl = Server.MapPath('sample.jpeg')

C#

//Function
public byte []  showImage(Int32 empid)  
{
	SqlConnection  myconnection  = new SqlConnection ('Server=localhost;uid=sa;password=;database=northwind;');
	SqlCommand mycommand = new SqlCommand ('Select Employeeid, FirstName,Photo from Employees where employeeid =' + empid, myconnection);
	myconnection.Open ();
	SqlDataReader  dr= mycommand.ExecuteReader();
	dr.Read();
	byte[]  imgbyte =(byte[]) dr['Photo']; 
	return imgbyte;
		
}
//In Page_Load
byte[] data  = showImage (2);
Int32  offset  =78;
System.IO.MemoryStream mstream = new System.IO.MemoryStream  ();
mstream.Write(data, offset, data.Length - offset);
System.Drawing.Bitmap bmp= new System.Drawing.Bitmap(mstream);
bmp.Save(Server.MapPath ('sample.jpeg'), System.Drawing.Imaging.ImageFormat.Jpeg );
mstream.Close();
Image1.ImageUrl = Server.MapPath('sample.jpeg');
Permalink
I am using GDI+ to write on a bitmap. I know how to make font Italic/Bold. What should be done to apply both Italic and Bold

VB.NET

dim fntFont as Font = new Font('Arial',16,FontStyle.Italic|FontStyle.Bold )

C#

Font fntFont =new Font('Arial',16,FontStyle.Italic|FontStyle.Bold ) ;

For more details refer FontStyle Enumeration

Permalink
Why do I get the error message ‘A generic error occurred in GDI+.’

Give the proper path of the file. Check your permissions on the server where you are trying to write/read the files

Permalink