TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Bhavesh Vankar
761
1.1k
88k
Capture Image by webcam getting error
Sep 10 2020 8:00 AM
In my project i want to capture image all done but now its generating error and image not capturing below is my code kindly help me to solve the error.
image not getting in folder using below code. kindly please solve and suggest me good code.
on my captureimage.aspx
jquery code for capture image
<script type=
"text/javascript"
>
var
pageUrl =
'<%=ResolveUrl("~/captureimage.aspx") %>'
;
$(
function
() {
Webcam.set({
width: 320,
height: 240,
image_format:
'jpeg'
,
jpeg_quality: 90
});
Webcam.attach(
'#webcam'
);
$(
"#btnCapture"
).click(
function
() {
Webcam.snap(
function
(data_uri) {
$(
"#imgCapture"
)[0].src = data_uri;
$(
"#btnUpload"
).removeAttr(
"disabled"
);
});
});
$(
"#btnCapture"
).click(
function
() {
$.ajax({
type:
"POST"
,
url: pageUrl +
"captureimage.aspx/GetCapturedImage"
,
data:
"{data: '"
+ $(
"#imgCapture"
)[0].src +
"'}"
,
contentType:
"application/json; charset=utf-8"
,
dataType:
"json"
,
success:
function
(r)
{
$(
"[id*=imgCapture]"
).css(
"visibility"
,
"visible"
);
$(
"[id*=imgCapture]"
).attr(
"src"
, r.d);
},
failure:
function
(response)
{
alert(response.d);
},
oncapture:
function
()
{
webcam.save(pageUrl);
}
});
});
});
function
capture()
{
webcam.capture();
return
false
;
}
</script>
my code in captureimage.cs file...
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!
this
.IsPostBack)
{
if
(Request.InputStream.Length > 0)
{
using
(StreamReader myreader =
new
StreamReader(Request.InputStream))
{
string
myhexString = Server.UrlEncode(myreader.ReadToEnd());
string
myimageName = DateTime.Now.ToString(
"Image.jpg"
);
string
myimagePath =
string
.Format (
"~/CapturedImage/Image.jpg"
, myimageName);
File.WriteAllBytes(Server.MapPath(myimagePath), ConvertHexToBytes(myhexString));
Session[
"visitorimage"
] = ResolveUrl(myimagePath);
}
}
}
}
private
static
byte
[] ConvertHexToBytes(
string
hex)
{
byte
[] bytes =
new
byte
[hex.Length / 2];
for
(
int
i = 0; i < hex.Length; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return
bytes;
}
[WebMethod(EnableSession =
true
)]
public
static
string
GetCapturedImage()
{
string
Imageurl = HttpContext.Current.Session [
"visitorimage"
].ToString();
HttpContext.Current.Session[
"visitorimage"
] =
null
;
return
Imageurl;
}
While Click on capture button occure error
"An exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll but was not handled in user code "
Crop code is..
protected
void
btnCrop_Click(
object
sender, EventArgs e)
{
string
FilePath = Path.Combine(
"~/CapturedImage/Image.jpg"
);
int
x = Convert.ToInt32(X.Value);
int
y = Convert.ToInt32(Y.Value);
int
w = Convert.ToInt32(W.Value);
int
h = Convert.ToInt32(H.Value);
//System.Drawing.Image image = Bitmap.FromFile(HttpContext.Current.Request.PhysicalApplicationPath + @"\CapturedImage\" + DateTime.Now.ToString("dd-MM-yy hh-mm") + ".jpg");
System.Drawing.Image image = Bitmap.FromFile(HttpContext.Current.Request.PhysicalApplicationPath + @
"\CapturedImage\Image.jpg"
);
Bitmap bmp =
new
Bitmap(w, h, image.PixelFormat);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(image,
new
Rectangle(0, 0, w, h),
new
Rectangle(x, y, w, h), GraphicsUnit.Pixel);
//bmp.Save(HttpContext.Current.Request.PhysicalApplicationPath + @"\CroppedImage\" + DateTime.Now.ToString("dd-MM-yy hh-mm") + ".jpg", image.RawFormat);
bmp.Save(HttpContext.Current.Request.PhysicalApplicationPath + @
"\CroppedImage\Image.jpg"
, image.RawFormat);
//imgCapture.ImageUrl = @"\CroppedImage\" + DateTime.Now.ToString("dd-MM-yy hh-mm") + ".jpg";
imgCapture.ImageUrl =
"~/CroppedImage/Image.jpg"
;
}
Reply
Answers (
3
)
What is the diff between Variable and Properties ?
Dynamic to static query conversion