Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts

How to check the specific field in the row has contain null or value.

Posted by Venkat | Labels: , ,

Good Morning after a month I spend the time to post the small tips who already known but i would like to share this post which everyone come to know.

Question :  how to check the specific field in the row has contain null or value.

Normally we used to get the data from the DB using specific id for ex: get the specific user details using his userid. So it will return that row value. In that row some field may contain NULL value.

Here is the code to check the particular field contains value or null.

In my case I am getting the row from Database and assigned to the Datarow like this

Dim dr as DataRow

dr = ds.Tables(0).Rows(0)

Here the dr contains the row data from this I am checking the field.

Datarow  contains a method called  IsNull to check whether the field has null or value.

EX:

if (dr.IsNull("fieldname")) then
' if the field contain null do the manipulation here
else
' the field cotains some value.
End if 

the above code defines the Datarow contain a method called IsNull which is going to check the whether the specified fieldname has value or null as per we can manipulate it.

And also I am going to tell another tips which was pointed @ DotnetCurry.com here - thanks for that.

IE: if we are going to display user photo on Gridview where user have to upload his photo which will be an optional, User may or may not upload his photo, at that time the user who don't have photo on DB , the Gridview shows the cross mark image for that user. This is not good in practice from user point of view  instead we have to show the default image for the user who don't have photo on DB.

Example:

on the HTML img tag write like this

< img src="Dynamic-imagename" onerror="this.src='../images/NoImageFound.jpg'" .. / > 

This above tag will show the user photo who have photo on DB if there is no photo on DB for those it shows the default image specified on onerror event.

Watermark of images

Posted by Venkat | Labels: ,

We are going to discuss about watermarking the image , the importance of doing the watermark is protected and not to be redistributed , Suppose if you take some online gallery images , like arts,some site which selling photos are restricted his site image. They might be not to allow users to save image by right click on image , or saving the web page.


'Creating Dynamic Watermark on image
Dim objImage As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("~/images/" & "Management.jpg"))
'From File
Dim height As Integer = objImage.Height
'Actual image width
Dim width As Integer = objImage.Width
'Actual image height
Dim bitmapimage As New System.Drawing.Bitmap(objImage, width, height)
' create bitmap with same size of Actual image
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmapimage)
'Creates a System.Drawing.Color structure from the four ARGB component
'(alpha, red, green, and blue) values. Although this method allows a 32-bit value
' to be passed for each component, the value of each component is limited to 8 bits.
'create Brush
Dim brush As New SolidBrush(Color.FromArgb(113, 255, 255, 255))
'Adding watermark text on image
g.DrawString("Copywright", New Font("Arial", 18, System.Drawing.FontStyle.Bold), brush, 0, 100)
'save image with Watermark image/picture
'bitmapimage.Save("watermark-image.jpg"); //if u want to save image
Response.ContentType = "image/jpeg"
bitmapimage.Save(Server.MapPath("~/images/Copy.jpg"), ImageFormat.Jpeg)
bitmapimage.Dispose()
objImage.Dispose()

Resize an image using asp.net (vb.net) - Method 2

Posted by Venkat | Labels:

This is the second method of dynamically resizing the image ,


Resize an image using asp.net (vb.net) while uploading it into the server
======================Copy following code to Invoke upload======================

Dim fileExt = System.IO.Path.GetExtension(FileUpload.FileName).ToLower()
Dim previewName As String = Me.txtName.Text & "_preview" & fileExt
If fileExt.ToString.ToLower = ".jpeg" Or fileExt.ToString.ToLower = ".jpg" Or
fileExt.ToString.ToLower = ".gif" Then
Dim previewPic As Bitmap = (ResizeImage(FileUpload.PostedFile.InputStream, 500,
695))
previewPic.Save(Server.MapPath("Images/preview/") & previewName,
ImageFormat.Jpeg)
previewPic.Dispose()
End If
==================Copy following Function for resizing image====================
Function ResizeImage(ByVal streamImage As Stream, ByVal maxWidth As Int32, ByVal
maxHeight As Int32) As Bitmap

Dim originalImage As New Bitmap(streamImage)
Dim newWidth As Int32 = originalImage.Width
Dim newHeight As Int32 = originalImage.Height
Dim aspectRatio As Double = Double.Parse(originalImage.Width) /
Double.Parse(originalImage.Height)
If (aspectRatio <= 1 And originalImage.Width > maxWidth) Then
newWidth = maxWidth
newHeight = CInt(Math.Round(newWidth / aspectRatio))
Else
If (aspectRatio > 1 And originalImage.Height > maxHeight) Then
newHeight = maxHeight
newWidth = CInt(Math.Round(newHeight * aspectRatio))
End If
End If
Dim newImage As New Bitmap(originalImage, newWidth, newHeight)
Dim g As Graphics = Graphics.FromImage(newImage)
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height)
originalImage.Dispose()
Return newImage
End Function

Resize Image using asp.net Method - 1

Posted by Venkat | Labels: ,

Here We have to discuss, how to dynamically resize the images using asp.net , generally we used two image one as Thumbnail and another one is big image , so if you upload the big image , we can set the default height and width on code to resize that image , at the same while we upload small image , on resizing the image it will be stretched to the specified height and width and also not seen qualify of image is not good ,

so here , we calculate the width , as per we resize the image

I would prefer this method.

this is the First Method , for this place one fileupload control and button on you design form


Dim thumbWidth As Integer = 132
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
'Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
Dim srcWidth As Integer = image.Width
Dim srcHeight As Integer = image.Height
Dim thumbHeight As Integer = (srcHeight / srcWidth) * thumbWidth
Dim bmp As New Bitmap(thumbWidth, thumbHeight)
'Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
'Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
'Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
'Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High

' Draw the original image into the target Graphics object scaling to the desired width and height
Dim rectDestination As New System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight)
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel)

'Save to destination file
bmp.Save(Server.MapPath("~/images/" & FileUpload1.PostedFile.FileName))

' dispose / release resources
bmp.Dispose()
image.Dispose()

pass the imagename to ImageUrl

Posted by Venkat | Labels: ,

Hi generally we are binding image to datalist..

so suppose if we store the image in the project folder

we have to get the name of the file and pass it on the src attribute of

img tag.. Ex:

<img src='< %#"BigImages/" & Eval("imagename") % > ' width ="485px" height="613px"  border="0" />
here bigimages is the foldername and imagename that we will get it from database..

so it will run properly..

Suppose if we do some task ie: hiding images - for that we have to

write the code in Datalist_ItemDatabound and also same for Gridview.. event has

been changed ie:ItemCommand..

At that if we use runat="server" id="Image1" for the above tag , while we get the

image through code it shows error

Unable to cast the html image to webcontrol image

for that use asp:image this is way you have to apply imageurl so it display the

image as per imagename - from the folder

<asp:Image ID="ProductImg"
CssClass="ProductImg"
runat="server"
ImageUrl=' < %# Eval("BigImages","imagename/{0}").Trim() % >'
AlternateText=' < %# Eval("imagename","").Trim() % >' 


PayOffers.in